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.
We'll cover the following...
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.
In this case, *p is equivalent ...