*Memo:
-
My post explains the iterable unpacking with
*
and a function (1). -
My post explains the iterable unpacking with
*
and a function (3). - 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 unpacking with a
for
statement (3). -
My post explains the dictionary unpacking with
**
and a 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(p=iterable(*iterable))>:
def func(p=[*[0, 1], 2, *[3, 4, *[5]]]): # list
# def func(p=[*range(4), *range(4, 6)]): # range
print(p)
func()
# [0, 1, 2, 3, 4, 5]
def func(p=(*(0, 1), 2, *(3, 4, *(5,)))): # tuple
print(p)
func()
# (0, 1, 2, 3, 4, 5)
def func(p={*{0, 1}, 2, *{3, 4, *{5}}}): # set
print(p)
func()
# {0, 1, 2, 3, 4, 5}
def func(p=frozenset({*frozenset({0, 1}), 2, # frozenset
*frozenset({3, 4, *frozenset({5})})})):
print(p)
func()
# frozenset({0, 1, 2, 3, 4, 5})
def func(p=iter([*iter([0, 1]), 2, # iterator
*iter([3, 4, *iter([5])])])):
print(p)
print(*p)
func()
# <list_iterator object at 0x000001977A83A6B0>
# 0 1 2 3 4 5
def func(p=[*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11}]): # dict
# def func(p=[*{0:1, 2:3, 4:5, 6:7}.keys(), # dict.keys()
# *{8:9, 10:11}.keys()]):
print(p)
func()
# [0, 2, 4, 6, 8, 10]
def func(p=[*{0:1, 2:3, 4:5, 6:7}.values(), # dict.values()
*{8:9, 10:11}.values()]):
print(p)
func()
# [1, 3, 5, 7, 9, 11]
def func(p=[*{0:1, 2:3, 4:5, 6:7}.items(), # dict.items()
*{8:9, 10:11}.items()]):
print(p)
func()
# [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)]
def func(p=[*'0123', *'45']): # str
print(p)
func()
# ['0', '1', '2', '3', '4', '5']
def func(p=[*b'0123', *b'45']): # bytes
# def func(p=[*bytearray(b'0123'), *bytearray(b'45')]): # bytearray
print(p)
func()
# [48, 49, 50, 51, 52, 53]
# list # bytes # set
def func(p=[*[0, 1], *b'2', *{3, 4, 5}]):
print(p)
func()
# [0, 1, 50, 3, 4, 5]
<Iterable unpacking with func(p=*iterable)>:
def func(p=*[0, 1, 2, 3, 4, 5]): pass # list
def func(p=*(0, 1, 2, 3, 4, 5)): pass # tuple
def func(p=*{0, 1, 2, 3, 4, 5}): pass # set
def func(p=*frozenset({0, 1, 2, 3, 4, 5})): pass # frozenset
def func(p=*{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}): pass # dict
def func(p=*{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.keys()): pass # dict.keys()
def func(p=*{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.values()): pass # dict.values()
def func(p=*{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.items()): pass # dict.items()
def func(p=*iter([0, 1, 2, 3, 4, 5])): pass # iterator
def func(p=*'012345'): pass # str
def func(p=*b'012345'): pass # bytes
def func(p=*bytearray(b'012345')): pass # bytearray
def func(p=*range(6)): pass # range
# SyntaxError: can't use starred expression here
<Iterable unpacking with func(p=dict(*iterable))>:
def func(p={'A':'B', *[0, 1, 2], 'C':'D'}): pass # list
def func(p={'A':'B', *(0, 1, 2), 'C':'D'}): pass # tuple
def func(p={'A':'B', *{0, 1, 2}, 'C':'D'}): pass # set
def func(p={'A':'B', *frozenset({0, 1, 2}), 'C':'D'}): pass # frozenset
def func(p={'A':'B', *{0:1, 2:3}, 'D':'E'}): pass # dict
def func(p={'A':'B', *{0:1, 2:3}.keys(), 'C':'D'}): pass # dict.keys()
def func(p={'A':'B', *{0:1, 2:3}.values(), 'C':'D'}): pass # dict.values()
def func(p={'A':'B', *{0:1, 2:3}.items(), 'C':'D'}): pass # dict.items()
def func(p={'A':'B', *iter([0, 1, 2]), 'C':'D'}): pass # iterator
def func(p={'A':'B', *'012', 'C':'D'}): pass # str
def func(p={'A':'B', *b'012', 'C':'D'}): pass # bytes
def func(p={'A':'B', *bytearray(b'012'), 'C':'D'}): pass # bytearray
def func(p={'A':'B', *range(3), 'C':'D'}): pass # range
# SyntaxError: invalid syntax
Top comments (0)