*Memos:
- My post explains iterable unpacking with a function.
*
can unpack an iterables as shown below:
*Memos:
-
*
is called iterable unpacking operator. - One or more
*iterables
can be used in a list, tuple or set literal but not in a dict(dictionary) literal. *One*iterable
cannot be directly used in the right side of=
in a variable assignment but it's possible if one or more*iterables
are in a list, tuple or set. - Zero or more
*iterables
can be used as the unpacked iterable arguments in a function call passed to the zero or more parameters including*args
but excluding**kwargs
in a function definition:-
*
of*args
in a function definition not unpacking an iterable isn't iterable unpacking operator, it's to create a var-positional parameter. **
of*args
in a function body unpacking an iterable is iterable unpacking operator. -
**
of**kwargs
in a function definition not unpacking a dictionary isn't dictionary unpacking operator, it's to create a var-keyword parameter. ***
of**kwargs
in a function body unpacking a dictionary is dictionary unpacking operator.
-
<Iterable unpacking>:
print(*['A', 'B', 'C', 'D']) # list
print(*('A', 'B', 'C', 'D')) # tuple
print(*{'A':'a', 'B':'b', 'C':'c', 'D':'d'}) # dict
print(*{'A':'a', 'B':'b', 'C':'c', 'D':'d'}.keys()) # dict keys()
print(*iter(['A', 'B', 'C', 'D'])) # iterator
print(*'ABCD') # str
# A B C D
print(*{'A', 'B', 'C', 'D'}) # set
print(*frozenset({'A', 'B', 'C', 'D'})) # frozenset
# B A D C
print(*{'A':'a', 'B':'b', 'C':'c', 'D':'d'}.values()) # dict values()
# a b c d
print(*{'A':'a', 'B':'b', 'C':'c', 'D':'d'}.items()) # dict items()
# ('A', 'a') ('B', 'b') ('C', 'c') ('D', 'd')
print(*b'ABCD') # bytes
print(*bytearray(b'ABCD')) # bytearray
# 65 66 67 68
# list # bytes # set
print(*['A', 'B', 'C', 'D'], *b'ABCD', *{'A', 'B', 'C', 'D'})
# A B C D 65 66 67 68 B A D C
# list
v = *['A', 'B', 'C', 'D']
# SyntaxError: can't use starred expression here
<Iterable unpacking in a list>:
print([1, 2, *['A', 'B', 'C'], 3, 4]) # list
print([1, 2, *('A', 'B', 'C'), 3, 4]) # tuple
print([1, 2, *{'A':'a', 'B':'b', 'C':'c'}, 3, 4]) # dict
print([1, 2, *{'A':'a', 'B':'b', 'C':'c'}.keys(), 3, 4]) # dict keys()
print([1, 2, *iter(['A', 'B', 'C']), 3, 4]) # iterator
print([1, 2, *'ABC', 3, 4]) # str
# [1, 2, 'A', 'B', 'C', 3, 4]
print([1, 2, *{'A', 'B', 'C'}, 3, 4]) # set
print([1, 2, *frozenset({'A', 'B', 'C'}), 3, 4]) # frozenset
# [1, 2, 'B', 'A', 'C', 3, 4]
print([1, 2, *{'A':'a', 'B':'b', 'C':'c'}.values(), 3, 4]) # dict values()
# [1, 2, 'a', 'b', 'c', 3, 4]
print([1, 2, *{'A':'a', 'B':'b', 'C':'c'}.items(), 3, 4]) # dict items()
# [1, 2, ('A', 'a'), ('B', 'b'), ('C', 'c'), 3, 4]
print([1, 2, *b'ABC', 3, 4]) # bytes
print([1, 2, *bytearray(b'ABC'), 3, 4]) # bytearray
# [1, 2, 65, 66, 67, 3, 4]
# list # set # str
v = [1, *['A', 'B', 'C'], 2, *{'A', 'B', 'C'}, 3, *'ABC', 4]
print(v)
# [1, 'A', 'B', 'C', 2, 'B', 'A', 'C', 3, 'A', 'B', 'C', 4]
<Iterable unpacking in a tuple>:
print((1, 2, *['A', 'B', 'C'], 3, 4)) # list
print((1, 2, *('A', 'B', 'C'), 3, 4)) # tuple
print((1, 2, *{'A':'a', 'B':'b', 'C':'c'}, 3, 4)) # dict
print((1, 2, *{'A':'a', 'B':'b', 'C':'c'}.keys(), 3, 4)) # dict keys()
print((1, 2, *iter(['A', 'B', 'C']), 3, 4)) # iterator
print((1, 2, *'ABC', 3, 4)) # str
# (1, 2, 'A', 'B', 'C', 3, 4)
print((1, 2, *{'A', 'B', 'C'}, 3, 4)) # set
print((1, 2, *frozenset({'A', 'B', 'C'}), 3, 4)) # frozenset
# (1, 2, 'B', 'A', 'C', 3, 4)
print((1, 2, *{'A':'a', 'B':'b', 'C':'c'}.values(), 3, 4)) # dict values()
# (1, 2, 'a', 'b', 'c', 3, 4)
print((1, 2, *{'A':'a', 'B':'b', 'C':'c'}.items(), 3, 4)) # dict items()
# (1, 2, ('A', 'a'), ('B', 'b'), ('C', 'c'), 3, 4)
print((1, 2, *b'ABC', 3, 4)) # bytes
print((1, 2, *bytearray(b'ABC'), 3, 4)) # bytearray
# (1, 2, 65, 66, 67, 3, 4)
# list # set # str
v = (1, *['A', 'B', 'C'], 2, *{'A', 'B', 'C'}, 3, *'ABC', 4)
print(v)
# (1, 'A', 'B', 'C', 2, 'B', 'A', 'C', 3, 'A', 'B', 'C', 4)
<Iterable unpacking in a set>:
print({1, 2, *['A', 'B', 'C'], 3, 4}) # list
print({1, 2, *('A', 'B', 'C'), 3, 4}) # tuple
print({1, 2, *{'A':'a', 'B':'b', 'C':'c'}, 3, 4}) # dict
print({1, 2, *{'A':'a', 'B':'b', 'C':'c'}.keys(), 3, 4}) # dict keys()
print({1, 2, *iter(['A', 'B', 'C']), 3, 4}) # iterator
print({1, 2, *'ABC', 3, 4}) # str
# (1, 2, 'A', 'B', 'C', 3, 4)
print({1, 2, *{'A', 'B', 'C'}, 3, 4}) # set
print({1, 2, *frozenset({'A', 'B', 'C'}), 3, 4}) # frozenset
# (1, 2, 'B', 'A', 'C', 3, 4)
print({1, 2, *{'A':'a', 'B':'b', 'C':'c'}.values(), 3, 4}) # dict values()
# (1, 2, 'a', 'b', 'c', 3, 4)
print({1, 2, *{'A':'a', 'B':'b', 'C':'c'}.items(), 3, 4}) # dict items()
# (1, 2, ('A', 'a'), ('B', 'b'), ('C', 'c'), 3, 4)
print({1, 2, *b'ABC', 3, 4}) # bytes
print({1, 2, *bytearray(b'ABC'), 3, 4}) # bytearray
# (1, 2, 65, 66, 67, 3, 4)
# list # set # str
v = {1, *['A', 'B', 'C'], 2, *{'A', 'B', 'C'}, 3, *'ABC', 4}
print(v)
# {1, 2, 3, 4, 'A', 'B', 'C'}
<Iterable unpacking in a dictionary>:
print({1:2, 3:*['A', 'B', 'C'], 4:5}) # list
print({1:2, 3:*('A', 'B', 'C'), 4:5}) # tuple
print({1:2, 3:*{'A', 'B', 'C'}, 4:5}) # set
print({1:2, 3:*frozenset({'A', 'B', 'C'}), 4:5}) # frozenset
print({1:2, 3:*{'A':'a', 'B':'b', 'C':'c'}, 4:5}) # dict
print({1:2, 3:*{'A':'a', 'B':'b', 'C':'c'}.keys(), 4:5}) # dict keys()
print({1:2, 3:*{'A':'a', 'B':'b', 'C':'c'}.values(), 4:5}) # dict values()
print({1:2, 3:*{'A':'a', 'B':'b', 'C':'c'}.items(), 4:5}) # dict items()
print({1:2, 3:*iter(['A', 'B', 'C']), 4:5}) # iterator
print({1:2, 3:*'ABC', 4:5}) # str
print({1:2, 3:*b'ABC', 4:5}) # bytes
print({1:2, 3:*bytearray(b'ABC'), 4:5}) # bytearray
# SyntaxError: cannot use a starred expression in a dictionary value
print({1:2, *['A', 'B', 'C']:3, 4:5}) # list
print({1:2, *('A', 'B', 'C'):3, 4:5}) # tuple
print({1:2, *{'A', 'B', 'C'}:3, 4:5}) # set
print({1:2, *frozenset({'A', 'B', 'C'}):3, 4:5}) # frozenset
print({1:2, *{'A':'a', 'B':'b', 'C':'c'}:3, 4:5}) # dict
print({1:2, *{'A':'a', 'B':'b', 'C':'c'}.keys():3, 4:5}) # dict keys()
print({1:2, *{'A':'a', 'B':'b', 'C':'c'}.values():3, 4:5}) # dict values()
print({1:2, *{'A':'a', 'B':'b', 'C':'c'}.items():3, 4:5}) # dict items()
print({1:2, *iter(['A', 'B', 'C']):3, 4:5}) # iterator
print({1:2, *'ABC':3, 4:5}) # str
print({1:2, *b'ABC':3, 4:5}) # bytes
print({1:2, *bytearray(b'ABC'):3, 4:5}) # bytearray
# SyntaxError: invalid syntax