DEV Community

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

Posted on • Edited on

Unpacking in Python (6)

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

Iterable unpacking can be done with * and a function as shown below:

<Iterable unpacking with func(p1, p2, p3, p4, p5, p6)>:

def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*[0, 1, 2, 3], *[4, 5])                       # list
func(*(0, 1, 2, 3), *(4, 5))                       # tuple
func(*{0, 1, 2, 3}, *{4, 5})                       # set
func(*frozenset({0, 1, 2, 3}), *frozenset({4, 5})) # frozenset
func(*iter([0, 1, 2, 3]), *iter([4, 5]))           # iterator
func(*'0123', *'45')                               # str
func(*range(4), *range(4, 6))                      # range
# 0 1 2 3 4 5

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

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

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

func(*b'0123', *b'45')                             # bytes
func(*bytearray(b'0123'), *bytearray(b'45'))       # bytearray
# 48 49 50 51 52 53

    # list  # bytes    # set
func(*[0, 1], *b'2', *{3, 4, 5})
# 0 1 50 3 4 5
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking with func(*args)>:

def func(*args):
    print(args)
    print(*args)
    print(['A', 'B', *args, 'C', 'D'])

func()
# ()
# Nothing
# ['A', 'B', 'C', 'D']

func(*[0, 1, 2, 3], *[4, 5])                       # list
func(*(0, 1, 2, 3), *(4, 5))                       # tuple
func(*{0, 1, 2, 3}, *{4, 5})                       # set
func(*frozenset({0, 1, 2, 3}), *frozenset({4, 5})) # frozenset
func(*iter([0, 1, 2, 3]), *iter([4, 5]))           # iterator
func(*range(4), *range(4, 6))                      # range
# (0, 1, 2, 3, 4, 5)
# 0 1 2 3 4 5
# ['A', 'B', 0, 1, 2, 3, 4, 5, 'C', 'D']

func(*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11})         # dict
func(*{0:1, 2:3, 4:5, 6:7}.keys(),                 # dict.keys()
     *{8:9, 10:11}.keys())
# (0, 2, 4, 6, 8, 10)
# 0 2 4 6 8 10
# ['A', 'B', 0, 2, 4, 6, 8, 10, 'C', 'D']

func(*{0:1, 2:3, 4:5, 6:7}.values(),               # dict.values()
     *{8:9, 10:11}.values())
# (1, 3, 5, 7, 9, 11)
# 1 3 5 7 9 11
# ['A', 'B', 1, 3, 5, 7, 9, 11, 'C', 'D']

func(*{0:1, 2:3, 4:5, 6:7}.items(),                # dict.items()
     *{8:9, 10:11}.items())
# ((0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11))
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11)
# ['A', 'B', (0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), 'C', 'D']

func(*'0123', *'45')                               # str
# ('0', '1', '2', '3', '4', '5')
# 0 1 2 3 4 5
# ['A', 'B', '0', '1', '2', '3', '4', '5', 'C', 'D']

func(*b'0123', *b'45')                             # bytes
func(*bytearray(b'0123'), *bytearray(b'45'))       # bytearray
# (48, 49, 50, 51, 52, 53)
# 48 49 50 51 52 53
# ['A', 'B', 48, 49, 50, 51, 52, 53, 'C', 'D']

    # list  # bytes    # set
func(*[0, 1], *b'2', *{3, 4, 5})
# (0, 1, 50, 3, 4, 5)
# 0 1 50 3 4 5
# ['A', 'B', 0, 1, 50, 3, 4, 5, 'C', 'D']
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking with func(p1, p2, *args)>:

def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, ['A', 'B', *args, 'C', 'D'])

func()
# a b ()
# a b
# a b ['A', 'B', 'C', 'D']

func(*[0, 1, 2, 3], *[4, 5])                       # list
func(*(0, 1, 2, 3), *(4, 5))                       # tuple
func(*{0, 1, 2, 3}, *{4, 5})                       # set
func(*frozenset({0, 1, 2, 3}), *frozenset({4, 5})) # frozenset
func(*iter([0, 1, 2, 3]), *iter([4, 5]))           # iterator
func(*range(4), *range(4, 6))                      # range
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']

func(*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11})         # dict
func(*{0:1, 2:3, 4:5, 6:7}.keys(),                 # dict.keys()
     *{8:9, 10:11}.keys())
# 0 2 (4, 6, 8, 10)
# 0 2 4 6 8 10
# 0 2 ['A', 'B', 4, 6, 8, 10, 'C', 'D']

func(*{0:1, 2:3, 4:5, 6:7}.values(),               # dict.values()
     *{8:9, 10:11}.values())
# 1 3 (5, 7, 9, 11)
# 1 3 5 7 9 11
# 1 3 ['A', 'B', 5, 7, 9, 11, 'C', 'D']

func(*{0:1, 2:3, 4:5, 6:7}.items(),                # dict.items()
     *{8:9, 10:11}.items())
# (0, 1) (2, 3) ((4, 5), (6, 7), (8, 9), (10, 11))
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11)
# (0, 1) (2, 3) ['A', 'B', (4, 5), (6, 7), (8, 9), (10, 11), 'C', 'D']

func(*'0123', *'45')                               # str
# 0 1 ('2', '3', '4', '5')
# 0 1 2 3 4 5
# 0 1 ['A', 'B', '2', '3', '4', '5', 'C', 'D']

func(*b'0123', *b'45')                             # bytes
func(*bytearray(b'0123'), *bytearray(b'45'))       # bytearray
# 48 49 (50, 51, 52, 53)
# 48 49 50 51 52 53
# 48 49 ['A', 'B', 50, 51, 52, 53, 'C', 'D']

    # list  # bytes    # set
func(*[0, 1], *b'2', *{3, 4, 5})
# 0 1 (50, 3, 4, 5)
# 0 1 50 3 4 5
# 0 1 ['A', 'B', 50, 3, 4, 5, 'C', 'D']
Enter fullscreen mode Exit fullscreen mode

<Iterable unpacking with func(**kwargs)>:

def func(**kwargs):
    print(kwargs)
    print(**kwargs)

func()
# {}
# Nothing

func(*[0, 1, 2, 3], *[4, 5])                       # list
func(*(0, 1, 2, 3), *(4, 5))                       # tuple
func(*{0, 1, 2, 3}, *{4, 5})                       # set
func(*frozenset({0, 1, 2, 3}), *frozenset({4, 5})) # frozenset
func(*iter([0, 1, 2, 3]), *iter([4, 5]))           # iterator
func(*range(4), *range(4, 6))                      # range
func(*{0:1, 2:3, 4:5, 6:7}.keys(),                 # dict.keys()
     *{8:9, 10:11}.keys())
func(*{0:1, 2:3, 4:5, 6:7}.values(),               # dict.values()
     *{8:9, 10:11}.values())
func(*{0:1, 2:3, 4:5, 6:7}.items(),                # dict.items()
     *{8:9, 10:11}.items())
func(*'0123', *'45')                               # str
func(*b'0123', *b'45')                             # bytes
func(*bytearray(b'0123'), *bytearray(b'45'))       # bytearray
    # list  # bytes    # set
func(*[0, 1], *b'2', *{3, 4, 5})
# TypeError: func() takes 0 positional arguments but 6 were given
Enter fullscreen mode Exit fullscreen mode

Top comments (0)