Unpacking an Iterable
Learn how to unpack an iterable into a parameter list or a sequence using the * operator.
We'll cover the following...
Unpacking an iterable to a parameter list
Here is a simple function that multiplies three numbers:
def mult3(a, b, c):
return a*b*c
x = mult3(2, 3, 5) # 30
Suppose the arguments you needed were already in a list. You can use the unpacking operator, *
, to “unpack” the values, as shown below.
Press + to interact
def mult3(a, b, c):return a*b*cp = [2, 3, 5]x = mult3(*p) # equivalent to mult3(2, 3, 5)print(x)
In this case, *p
is equivalent ...