DEV Community

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

Posted on • Edited on

Unpacking in Python (5)

Buy Me a Coffee

*Memo:

  • My post explains the iterable unpacking with * and a function (2).
  • 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 dictionary unpacking with ** within a dictionary and function.
  • My post explains *args.
  • My post explains **kwargs.

Iterable unpacking can be done with * within print() as shown below:

*Memo:

  • One or more *iterables can be used as the arguments within a function call including print() to unpack them into the one or more parameters including *args but excluding **kwargs within a function definition and used within a list, tuple or set to unpack iterables:
    • One or more *iterables can be used with print() and *args but not with **kwargs.
  • The only one parameter with * which is *args conventionally can be used within a function definition:
    • A *args is the tuple parameter which can flexibly accept zero or more positional arguments so the type is tuple.
    • A *args is called a var-positional parameter.
    • A *args is the optional parameter with the immutable default value () so the default value () cannot be changed with =.
    • All the parameters after *args are keyword-only parameters so keyword arguments must be set to them.
    • *args can be other names like *teachers, *students, etc.
  • A * is called an iterable unpacking operator to unpack an iterable as the name suggests so the one or more *iterables used as the arguments within a function call including print() and used within a list, tuple or set to unpack iterables are iterable unpacking operators but the *args used within a function definition to flexibly accept zero or more positional arguments isn't an iterable unpacking operator:
    • The iterable unpacking operator * can unpack(flat) only the most outer dimension of an iterable.

<Iterable unpacking>:

print(*[0, 1, *[2]], *[3, 4])                          # list
print(*(0, 1, *(2,)), *(3, 4))                         # tuple
print(*{0, 1, *{2}}, *{3, 4})                          # set
print(*frozenset({0, 1, *frozenset({2}),               # frozenset
      *frozenset({3, 4})}))
print(*iter([0, 1, *iter([2]), *iter([3, 4])]))        # iterator
print(*'012', *'34')                                   # str
print(*range(3), *range(3, 5))                         # range
# 0 1 2 3 4

print(*{0:1, 2:3, 4:5}, *{6:7, 8:9})                   # dict
print(*{0:1, 2:3, 4:5}.keys(), *{6:7, 8:9}.keys())     # dict.keys()
# 0 2 4 6 8

print(*{0:1, 2:3, 4:5}.values(), *{6:7, 8:9}.values()) # dict.values()
# 1 3 5 7 9

print(*{0:1, 2:3, 4:5}.items(), *{6:7, 8:9}.items())   # dict.items()
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9)

print(*b'012', *b'34')                                 # bytes
print(*bytearray(b'012'), *bytearray(b'34'))           # bytearray
# 48 49 50 51 52

              # list          # bytes            # set
print(*[0, 1, *[2, 3, *[4]]], *b'01234', *{0, 1, *{2, 3, *{4}}})
# 0 1 2 3 4 48 49 50 51 52 0 1 2 3 4
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking within a list>:

print([*[0, 1, *[2]], *[3, 4]])                          # list
print([*(0, 1, *(2,)), *(3, 4)])                         # tuple
print([*{0, 1, *{2}}, *{3, 4}])                          # set
print([*frozenset({0, 1, *frozenset({2})}),              # frozenset
       *frozenset({3, 4})])
print([*iter([0, 1, *iter([2])]), *iter([3, 4])])        # iterator
print([*range(3), *range(3, 5)])                         # range
# [0, 1, 2, 3, 4]

print([*{0:1, 2:3, 4:5}, *{6:7, 8:9}])                   # dict
print([*{0:1, 2:3, 4:5}.keys(), *{6:7, 8:9}.keys()])     # dict.keys()
# [0, 2, 4, 6, 8]

print([*{0:1, 2:3, 4:5}.values(), *{6:7, 8:9}.values()]) # dict.values()
# [1, 3, 5, 7, 9]

print([*{0:1, 2:3, 4:5}.items(), *{6:7, 8:9}.items()])   # dict.items()
# [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]

print([*'012', *'34'])                                   # str
# ['0', '1', '2', '3', '4']

print([*b'012', *b'34'])                                 # bytes
print([*bytearray(b'012'), *bytearray(b'34')])           # bytearray
# [48, 49, 50, 51, 52]

               # list          # bytes            # set
print([*[0, 1, *[2, 3, *[4]]], *b'01234', *{0, 1, *{2, 3, *{4}}}])
# [0, 1, 2, 3, 4, 48, 49, 50, 51, 52, 0, 1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking within a tuple>:

print((*[0, 1, *[2]], *[3, 4]))                          # list
print((*(0, 1, *(2,)), *(3, 4)))                         # tuple
print((*{0, 1, *{2}}, *{3, 4}))                          # set
print((*frozenset({0, 1, *frozenset({2})}),              # frozenset
       *frozenset({3, 4})))
print((*iter([0, 1, *iter([2])]), *iter([3, 4])))        # iterator
print((*range(3), *range(3, 5)))                         # range
# (0, 1, 2, 3, 4)

print((*{0:1, 2:3, 4:5}, *{6:7, 8:9}))                   # dict
print((*{0:1, 2:3, 4:5}.keys(), *{6:7, 8:9}.keys()))     # dict.keys()
# (0, 2, 4, 6, 8)

print((*{0:1, 2:3, 4:5}.values(), *{6:7, 8:9}.values())) # dict.values()
# (1, 3, 5, 7, 9)

print((*{0:1, 2:3, 4:5}.items(), *{6:7, 8:9}.items()))   # dict.items()
# ((0, 1), (2, 3), (4, 5), (6, 7), (8, 9))

print((*'012', *'34'))                                   # str
# ('0', '1', '2', '3', '4')

print((*b'012', *b'34'))                                 # bytes
print((*bytearray(b'012'), *bytearray(b'34')))           # bytearray
# (48, 49, 50, 51, 52)

               # list          # bytes            # set
print((*[0, 1, *[2, 3, *[4]]], *b'01234', *{0, 1, *{2, 3, *{4}}}))
# (0, 1, 2, 3, 4, 48, 49, 50, 51, 52, 0, 1, 2, 3, 4)
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking within a set>:

print({*[0, 1, *[2]], *[3, 4]})                          # list
print({*(0, 1, *(2,)), *(3, 4)})                         # tuple
print({*{0, 1, *{2}}, *{3, 4}})                          # set
print({*frozenset({0, 1, *frozenset({2})}),              # frozenset
       *frozenset({3, 4})})
print({*iter([0, 1, *iter([2])]), *iter([3, 4])})        # iterator
print({*range(3), *range(3, 5)})                         # range
# {0, 1, 2, 3, 4}

print({*{0:1, 2:3, 4:5}, *{6:7, 8:9}})                   # dict
print({*{0:1, 2:3, 4:5}.keys(), *{6:7, 8:9}.keys()})     # dict.keys()
# {0, 2, 4, 6, 8}

print({*{0:1, 2:3, 4:5}.values(), *{6:7, 8:9}.values()}) # dict.values()
# {1, 3, 5, 7, 9}

print({*{0:1, 2:3, 4:5}.items(), *{6:7, 8:9}.items()})   # dict.items()
# {(0, 1), (2, 3), (6, 7), (4, 5), (8, 9)}

print({*'012', *'34'})                                   # str
# {'3', '2', '1', '4', '0'}

print({*b'012', *b'34'})                                 # bytes
print({*bytearray(b'012'), *bytearray(b'34')})           # bytearray
# {48, 49, 50, 51, 52}

               # list          # bytes            # set
print({*[0, 1, *[2, 3, *[4]]], *b'01234', *{0, 1, *{2, 3, *{4}}}})
# {0, 1, 2, 3, 4, 48, 49, 50, 51, 52}
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking within a dictionary>:

print({'A':'B', *[0, 1, 2]:'C', 'D':'E'})                # list
print({'A':'B', *(0, 1, 2):'C', 'D':'E'})                # tuple
print({'A':'B', *{0, 1, 2}:'C', 'D':'E'})                # set
print({'A':'B', *frozenset({0, 1, 2}):'C', 'D':'E'})     # frozenset
print({'A':'B', *{0:1, 2:3, 4:5}:'C', 'D':'E'})          # dict
print({'A':'B', *{0:1, 2:3, 4:5}.keys():'C', 'D':'E'})   # dict.keys()
print({'A':'B', *{0:1, 2:3, 4:5}.values():'C', 'D':'E'}) # dict.values()
print({'A':'B', *{0:1, 2:3, 4:5}.items():'C', 'D':'E'})  # dict.items()
print({'A':'B', *iter([0, 1, 2]):'C', 'D':'E'})          # iterator
print({'A':'B', *'012':'C', 'D':'E'})                    # str
print({'A':'B', *b'012':'C', 'D':'E'})                   # bytes
print({'A':'B', *bytearray(b'012'):'C', 'D':'E'})        # bytearray
print({'A':'B', *range(3):'C', 'D':'E'})                 # range
# SyntaxError: invalid syntax

print({'A':'B', 'C':*[0, 1, 2], 'D':'E'})                # list
print({'A':'B', 'C':*(0, 1, 2), 'D':'E'})                # tuple
print({'A':'B', 'C':*{0, 1, 2}, 'D':'E'})                # set
print({'A':'B', 'C':*frozenset({0, 1, 2}), 'D':'E'})     # frozenset
print({'A':'B', 'C':*{0:1, 2:3, 4:5}, 'D':'E'})          # dict
print({'A':'B', 'C':*{0:1, 2:3, 4:5}.keys(), 'D':'E'})   # dict.keys()
print({'A':'B', 'C':*{0:1, 2:3, 4:5}.values(), 4:5})     # dict.values()
print({'A':'B', 'C':*{0:1, 2:3, 4:5}.items(), 4:5})      # dict.items()
print({'A':'B', 'C':*iter([0, 1, 2]), 4:5})              # iterator
print({'A':'B', 'C':*'012', 4:5})                        # str
print({'A':'B', 'C':*b'012', 4:5})                       # bytes
print({'A':'B', 'C':*bytearray(b'012'), 4:5})            # bytearray
print({'A':'B', 'C':*range(3), 4:5})                     # range
# SyntaxError: cannot use a starred expression in a dictionary value
Enter fullscreen mode Exit fullscreen mode

Top comments (0)