Imagine we have a very simple function that adds three numebrs together:
def add_three_numbers (a, b, c):
return a + b + c
We would normally pass separate numebrs to the function, e.g.
add_three_numbers (10, 20, 35)
But what if our numbers are in a list or a tuple. If we pass that as a list then we are passing only a single argument, and the function declares an error:
my_list = [10, 20, 35]
add_three_numbers (my_list)
Python allows us to pass the list or tuple with the instruction to unpack it for input into the function. We instruct Python to unpack the list/tuple with an asterix before the list/tuple:
add_three_numbers (*my_list)
One thought on “83. Automatically passing unpacked lists or tuples to a function (or why do you see * before lists and tuples)”