The effect of using the *
operator on an argument when calling a function is that of unpacking the list or a tuple argument
def print_args(arg1, arg2):
print(str(arg1) + str(arg2))
a = [1,2]
b = tuple([3,4])
print_args(*a)
# 12
print_args(*b)
# 34
Note that the length of the starred argument need to be equal to the number of the function's arguments.
A common python idiom is to use the unpacking operator *
with the zip
function to reverse its effects:
a = [1,3,5,7,9]
b = [2,4,6,8,10]
zipped = zip(a,b)
# [(1,2), (3,4), (5,6), (7,8), (9,10)]
zip(*zipped)
# (1,3,5,7,9), (2,4,6,8,10)