* for Iterable Unpacking in a Python function
Buy Me a Coffee☕ *Memos: My post explains iterable unpacking in Python. My post explains variable assignment in Python. You can use * for the iterable unpacking in a function as shown below: *Memos: A * is an iterable unpacking operator. A *parameter can store zero or more values as a tuple in a function. Only one *parameter can be used for the iterable unpacking of a function. Zero or more *iterables can be passed to a function with the zero or more values unpacked from zero or more iterables. def func(*args): pass def func(*mylist): pass def func(p, *args): pass def func(p="param", *args): pass def func(*args, p): pass def func(*args, p="param"): pass def func(*args, **kwargs): pass def func(p, *args, **kwargs): pass def func(p="param", *args, **kwargs): pass def func(*args, p, **kwargs): pass def func(*args, p="param", **kwargs): pass # No error def func(*args1, *args2): pass # SyntaxError: * argument may appear only once def func(**kwargs, *args): pass # SyntaxError: arguments cannot follow var-keyword argument def func(*args="John"): pass def func(*args=["John"]): pass def func(*args=*["John"]): pass # SyntaxError: var-positional argument cannot have default value def func(*args): print(args, *args) func() func(*[]) func(*()) func(*{}) func(**{}) # () Nothing func({}) # ({},) {} func(0) func(*[0]) func(*(0,)) # (0,) 0 func(0, 1) func(*[0, 1]) func(*(0, 1)) # (0, 1) 0 1 func(0, 1, 2) func(*[0, 1, 2]) func(*(0, 1, 2)) # (0, 1, 2) 0 1 2 func(0, 1, 2, 3) func(*[0, 1, 2, 3]) func(*(0, 1, 2, 3)) # (0, 1, 2, 3) 0 1 2 3 func(0, 1, 2, 3, 4) func(*[0, 1, 2, 3, 4]) func(*(0, 1, 2, 3, 4)) # (0, 1, 2, 3, 4) 0 1 2 3 4 func([]) # ([],) [] func([0]) # ([0],) [0] func([0, 1]) # ([0, 1],) [0, 1] func([0, 1, 2]) # ([0, 1, 2],) [0, 1, 2] func([0, 1, 2, 3]) # ([0, 1, 2, 3],) [0, 1, 2, 3] func([0, 1, 2, 3, 4]) # ([0, 1, 2, 3, 4],) [0, 1, 2, 3, 4] func(()) # ((),) () func((0,)) # ((0,),) (0,) func((0, 1)) # ((0, 1),) (0, 1) func((0, 1, 2)) # ((0, 1, 2),) (0, 1, 2) func((0, 1, 2, 3)) # ((0, 1, 2, 3),) (0, 1, 2, 3) func((0, 1, 2, 3, 4)) # ((0, 1, 2, 3, 4),) (0, 1, 2, 3, 4) func({"fname":"John"}, {"lname":"Smith"}) # ({'fname': 'John'}, {'lname': 'Smith'}) {'fname': 'John'} {'lname': 'Smith'} func({"fname":"John", "lname":"Smith"}) # ({'fname': 'John', 'lname': 'Smith'},) {'fname': 'John', 'lname': 'Smith'} func(*{"fname":"John", "lname":"Smith"}) func(*{"fname":"John"}, *{"lname":"Smith"}) # ('fname', 'lname') fname lname def func(p1="p1", p2="p2", *args, p3="p3"): print(p1, p2, args, p3) func() func(*[]) func(*()) func(*{}) func(**{}) # p1 p2 () p3 func({}) # {} p2 () p3 func(0) func(*[0]) func(*(0,)) # 0 p2 () p3 func(0, 1) func(*[0, 1]) func(*(0, 1))0 1 () p3 # 0 1 () p3 func(0, 1, 2) func(*[0, 1, 2]) func(*(0, 1, 2)) # 0 1 (2,) p3 func(0, 1, 2, 3) func(*[0, 1, 2, 3]) func(*(0, 1, 2, 3)) # 0 1 (2, 3) p3 func(0, 1, 2, 3, 4) func(*[0, 1, 2, 3, 4]) func(*(0, 1, 2, 3, 4)) # 0 1 (2, 3, 4) p3 func([]) # [] p2 () p3 func([0]) # [0] p2 () p3 func([0, 1]) # [0, 1] p2 () p3 func([0, 1, 2]) # [0, 1, 2] p2 () p3 func([0, 1, 2, 3]) # [0, 1, 2, 3] p2 () p3 func([0, 1, 2, 3, 4]) # [0, 1, 2, 3, 4] p2 () p3 func(()) # () p2 () p3 func((0,)) # (0,) p2 () p3 func((0, 1)) # (0, 1) p2 () p3 func((0, 1, 2)) # (0, 1, 2) p2 () p3 func((0, 1, 2, 3)) # (0, 1, 2, 3) p2 () p3 func((0, 1, 2, 3, 4)) # (0, 1, 2, 3, 4) p2 () p3 func({"fname":"John"}, {"lname":"Smith"}) # {'fname': 'John'} {'lname': 'Smith'} () p3 func({"fname":"John", "lname":"Smith"}) # {'fname': 'John', 'lname': 'Smith'} p2 () p3 func(*{"fname":"John", "lname":"Smith"}) func(*{"fname":"John"}, *{"lname":"Smith"}) # fname lname () p3

*Memos:
You can use *
for the iterable unpacking in a function as shown below:
*Memos:
- A
*
is an iterable unpacking operator. - A
*parameter
can store zero or more values as a tuple in a function. - Only one
*parameter
can be used for the iterable unpacking of a function. - Zero or more
*iterables
can be passed to a function with the zero or more values unpacked from zero or more iterables.
def func(*args): pass
def func(*mylist): pass
def func(p, *args): pass
def func(p="param", *args): pass
def func(*args, p): pass
def func(*args, p="param"): pass
def func(*args, **kwargs): pass
def func(p, *args, **kwargs): pass
def func(p="param", *args, **kwargs): pass
def func(*args, p, **kwargs): pass
def func(*args, p="param", **kwargs): pass
# No error
def func(*args1, *args2): pass
# SyntaxError: * argument may appear only once
def func(**kwargs, *args): pass
# SyntaxError: arguments cannot follow var-keyword argument
def func(*args="John"): pass
def func(*args=["John"]): pass
def func(*args=*["John"]): pass
# SyntaxError: var-positional argument cannot have default value
def func(*args):
print(args, *args)
func()
func(*[])
func(*())
func(*{})
func(**{})
# () Nothing
func({})
# ({},) {}
func(0)
func(*[0])
func(*(0,))
# (0,) 0
func(0, 1)
func(*[0, 1])
func(*(0, 1))
# (0, 1) 0 1
func(0, 1, 2)
func(*[0, 1, 2])
func(*(0, 1, 2))
# (0, 1, 2) 0 1 2
func(0, 1, 2, 3)
func(*[0, 1, 2, 3])
func(*(0, 1, 2, 3))
# (0, 1, 2, 3) 0 1 2 3
func(0, 1, 2, 3, 4)
func(*[0, 1, 2, 3, 4])
func(*(0, 1, 2, 3, 4))
# (0, 1, 2, 3, 4) 0 1 2 3 4
func([])
# ([],) []
func([0])
# ([0],) [0]
func([0, 1])
# ([0, 1],) [0, 1]
func([0, 1, 2])
# ([0, 1, 2],) [0, 1, 2]
func([0, 1, 2, 3])
# ([0, 1, 2, 3],) [0, 1, 2, 3]
func([0, 1, 2, 3, 4])
# ([0, 1, 2, 3, 4],) [0, 1, 2, 3, 4]
func(())
# ((),) ()
func((0,))
# ((0,),) (0,)
func((0, 1))
# ((0, 1),) (0, 1)
func((0, 1, 2))
# ((0, 1, 2),) (0, 1, 2)
func((0, 1, 2, 3))
# ((0, 1, 2, 3),) (0, 1, 2, 3)
func((0, 1, 2, 3, 4))
# ((0, 1, 2, 3, 4),) (0, 1, 2, 3, 4)
func({"fname":"John"}, {"lname":"Smith"})
# ({'fname': 'John'}, {'lname': 'Smith'}) {'fname': 'John'} {'lname': 'Smith'}
func({"fname":"John", "lname":"Smith"})
# ({'fname': 'John', 'lname': 'Smith'},) {'fname': 'John', 'lname': 'Smith'}
func(*{"fname":"John", "lname":"Smith"})
func(*{"fname":"John"}, *{"lname":"Smith"})
# ('fname', 'lname') fname lname
def func(p1="p1", p2="p2", *args, p3="p3"):
print(p1, p2, args, p3)
func()
func(*[])
func(*())
func(*{})
func(**{})
# p1 p2 () p3
func({})
# {} p2 () p3
func(0)
func(*[0])
func(*(0,))
# 0 p2 () p3
func(0, 1)
func(*[0, 1])
func(*(0, 1))0 1 () p3
# 0 1 () p3
func(0, 1, 2)
func(*[0, 1, 2])
func(*(0, 1, 2))
# 0 1 (2,) p3
func(0, 1, 2, 3)
func(*[0, 1, 2, 3])
func(*(0, 1, 2, 3))
# 0 1 (2, 3) p3
func(0, 1, 2, 3, 4)
func(*[0, 1, 2, 3, 4])
func(*(0, 1, 2, 3, 4))
# 0 1 (2, 3, 4) p3
func([])
# [] p2 () p3
func([0])
# [0] p2 () p3
func([0, 1])
# [0, 1] p2 () p3
func([0, 1, 2])
# [0, 1, 2] p2 () p3
func([0, 1, 2, 3])
# [0, 1, 2, 3] p2 () p3
func([0, 1, 2, 3, 4])
# [0, 1, 2, 3, 4] p2 () p3
func(())
# () p2 () p3
func((0,))
# (0,) p2 () p3
func((0, 1))
# (0, 1) p2 () p3
func((0, 1, 2))
# (0, 1, 2) p2 () p3
func((0, 1, 2, 3))
# (0, 1, 2, 3) p2 () p3
func((0, 1, 2, 3, 4))
# (0, 1, 2, 3, 4) p2 () p3
func({"fname":"John"}, {"lname":"Smith"})
# {'fname': 'John'} {'lname': 'Smith'} () p3
func({"fname":"John", "lname":"Smith"})
# {'fname': 'John', 'lname': 'Smith'} p2 () p3
func(*{"fname":"John", "lname":"Smith"})
func(*{"fname":"John"}, *{"lname":"Smith"})
# fname lname () p3