DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Unpacking in Python (7)

Buy Me a Coffee

*Memo:

  • My post explains the iterable unpacking with * and a function (1).
  • My post explains the unpacking with an assignment statement (1).
  • My post explains the unpacking with an assignment statement (2).
  • My post explains the unpacking with a for statement (1).
  • My post explains the unpacking with a for statement (2).
  • My post explains the iterable unpacking with * and a function (1).
  • My post explains the iterable unpacking with * and a function (2).

Dictionary unpacking can be done with ** within a dictionary and function as shown below:

*Memo:

  • One or more **dictionaries can be used within a dictionary and as the arguments within a function call excluding print() to unpack them into the one or more parameters including **kwargs but excluding *args within a function definition and used within a list, tuple, set, frozenset or iterator to unpack iterables:
    • One or more **dictionaries can be used with **kwargs but not with print() and *args.
  • The only one parameter with which is kwargs conventionally can be used within a function definition:
    • A **kwargs is the dictionary parameter which can flexibly accepts zero or more keyward arguments so the type is dict.
    • A **kwargs is called a var-keyword parameter.
    • A **kwargs is the optional parameter with the immutable default value {} so the default value {} cannot be changed with =.
    • A **kwargs must be the last parameter.
    • A **kwargs can be other names like **teachers, **students, etc.
  • A ** is called a dictionary unpacking operator to unpack a dictionary as the name suggests so the one or more **dictionaries used within a dictionary and as the arguments within a function call excluding print() to unpack dictionaries are dictionary unpacking operators:
    • The dictionary unpacking operator ** can unpack(flat) only the most outer dimension of a dictionary.
print({0:1, **{'A':'B', 'C':'D'}, 2:3})          # dict(dict)
# {0: 1, 'A': 'B', 'C': 'D', 2: 3}

print({0:1, **{'A':'B', 'C':'D'}.keys(), 2:3})   # dict(dict.keys())
# TypeError: 'dict_keys' object is not a mapping

print({0:1, **{'A':'B', 'C':'D'}.values(), 2:3}) # dict(dict.values())
# TypeError: 'dict_values' object is not a mapping

print({0:1, **{'A':'B', 'C':'D'}.items(), 2:3})  # dict(dict.items())
# TypeError: 'dict_items' object is not a mapping

print(**{'A':'B', 'C':'D'})
# TypeError: 'A' is an invalid keyword argument for print()

print(**{0:1, 2:3})
# TypeError: keywords must be strings

**v = {'A':'B', 'C':'D'}
v = **{'A':'B', 'C':'D'}
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode
**['A', 'B', 'C']            # list
**('A', 'B', 'C')            # tuple
**{'A', 'B', 'C'}            # set
**frozenset({'A', 'B', 'C'}) # frozenset
**iterator(['A', 'B', 'C'])  # iterator
**'ABC'                      # str
**b'ABC'                     # bytes
**bytearray(b'ABC')          # bytearray
**range(3)                   # range
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode
def func(A='b', C='d'):
    print(A, C)

func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
func(A='B', C='D')
# B D

func()
# b d
Enter fullscreen mode Exit fullscreen mode
def func(**kwargs):
    print(kwargs)
    print({0:1, **kwargs, 2:3})

func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
func(A='B', C='D')
# {'A': 'B', 'C': 'D'}
# {0: 1, 'A': 'B', 'C': 'D', 2: 3}

func()
# {}
# {0: 1, 2: 3}
Enter fullscreen mode Exit fullscreen mode
def func(A='b', **kwargs):
    print(A, kwargs)
    print(A, {0:1, **kwargs, 2:3})

func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
func(A='B', C='D')
# B {'C': 'D'}
# B {0: 1, 'C': 'D', 2: 3}

func()
# b {}
# b {0: 1, 2: 3}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)