Unpacking iterables into function arguments PREMIUM

In Python, you can unpack the items in an iterable into separate positional arguments in a function (sometimes called "variadic arguments"). This works even if you don't know how long the iterable is.

Trey Hunner Trey Hunner 2 min read 01:49 video Python 3.10—3.14
Watch this as a 01:49 screencast
Same content, narrated. Your preference is remembered for next time.
Watch it

In Python, you can unpack the items in an iterable into separate positional arguments in a function, even if you don't know how long the iterable is.

Unpacking iterables of known length into function arguments

Here we have a list called things and we want to print out each item in that list:

>>> things = ["duck", "keyboard", "mug", "computer"]

But what if we don't want to print out something that looks like a list:

>>> print(things)
['duck', 'keyboard', 'mug', 'computer']

Instead, we want to print out each item individually with a space in between each one.

The print function can actually do this for us if we pass in each item separately to print:

>>> print(things[0], things[1], things[2], things[3])
duck keyboard mug computer

But this code only makes sense if we actually know that our list has exactly four items in it. If we don't know how many items will be in our list when we write our code, this isn't going to work.

Using * to unpack iterables into a function call

We need a way to pass in each of the items in our list individually (as separate arguments to the print function) even if we don't know how many items are in the list.

In Python, the * operator can do exactly that:

>>> print(*things)
duck keyboard mug computer

When we use the * operator while calling a function, we're telling Python that the thing after that * is an iterable. Python then loops over that iterable, takes each of the items in it, and passes them in as separate arguments to that function.

This isn't just syntactic sugar: this is the only way to unpack an iterable into separate function arguments.

The * operator works on any iterable

This * operator doesn't just work on lists: it works on any iterable. For example, we could use it on generator objects:

>>> squares = (n**2 for n in range(7))
>>> print(*squares)
0 1 4 9 16 25 36

The * operator works with any function

While * is most often used on functions that accept an unlimited number of positional arguments, we can use it on any function.

For example, we could use it with Python's range function. Here we're unpacking a two-item tuple (five_to_ten) into the two arguments in the range function:

>>> five_to_ten = (5, 10)
>>> range(*five_to_ten)
range(5, 10)

And here we're unpacking a three-item list (tens_up_to_fifty) into three arguments when calling the range function:

>>> tens_up_to_fifty = [0, 50, 10]
>>> range(*tens_up_to_fifty)
range(0, 50, 10)

Summary

You can use the * operator in Python to take each of the items in an iterable and pass them in as separate positional arguments in a function call.

Next up
Mutable default arguments
03:47 watch

In Python, default argument values are defined only one time (when a function is defined).

Watch

Now it's your turn! 🚀

We don't learn by reading or watching. We learn by doing. Practice this topic by working on these related Python exercises.

This is a free preview of a premium screencast. You have 1 preview remaining.