Search⌘ K
AI Features

Unpacking an Iterable

Explore how to unpack iterables to pass values as separate arguments to functions and to construct lists, tuples, and sets in Python. Learn techniques for extended unpacking with multiple iterables and interleaving values, enhancing your ability to work flexibly with Python's iterators and iterables.

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.

svg viewer
Python 3.8
def mult3(a, b, c):
return a*b*c
p = [2, 3, 5]
x = mult3(*p) # equivalent to mult3(2, 3, 5)
print(x)

In this case, *p is equivalent ...