Tuple unpacking in Python

Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video

Let's talk about how to unpack a tuple in Python. We'll compare tuple unpacking to indexing tuples, both for code clarity and correctness.

An alternative to hard-coded indexes

We have a three-item tuple, called p:

>>> p = (2, 1, 3)

We can access each of the things in this tuple by indexing it:

>>> print(p[0], p[1], p[2])
2 1 3

But we could also give names to the things in this tuple:

>>> x, y, z = p

This is called tuple unpacking. We've taken a three-item tuple and unpacked that tuple it into three variables (x, y, and z):

>>> print(x, y, z)
2 1 3

You can think of creating a tuple as packing values together and unpacking a tuple as undoing that work. We're unpacking each of the values in our tuple above into separate variable names.

If we try to unpack a three-item tuple into just two variables, we'll get an error:

>>> x, y = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

Tuple unpacking describes the shape of the tuple you're unpacking. So the number of variables you're unpacking into must be the same as the number of items in the tuple-to-be-unpacked.

Tuple unpacking is really handy for avoiding hard-coded indexes and instead, giving descriptive names to each of the things inside a tuple.

Unpacking without an equals sign

Tuple unpacking is most often used, not with an equals sign, but instead in a for loop.

If we call the items method on a dictionary, we'll get an iterable of two-item tuples:

>>> things = {"ducks": 2, "lamps": 3, "chairs": 0}
>>> things.items()
dict_items([('ducks', 2), ('lamps', 3), ('chairs', 0)])

These tuples represent the key-value pairs in our dictionary.

>>> for item in things.items():
...     print(item)
('ducks', 2)
('lamps', 3)
('chairs', 0)

We already know that we can unpack each of the things in each of these key-value tuples into two variables (say thing and count):

>>> for item in things.items():
...     thing, count = item
...     print(thing, count)
ducks 2
lamps 3
chairs 0

But we actually don't even need an equals sign to do tuple unpacking.

Every iteration of a for loop does an implicit assignment. The thing between the for and the in in a for loop, is very similar to the thing on the left-hand side of an equal sign in an assignment statement.

We can actually do that tuple unpacking right in the for line:

>>> for thing, count in things.items():
...     print(thing, count)
ducks 2
lamps 3
chairs 0

We didn't need that item variable at all.

We can do the tuple unpacking right inside the for loop itself because anything you can put on the left-hand side of the equal sign, you can put in between the for and the in in a for loop. This is the most common place you'll see tuple unpacking used: on the first line of a for loop.

Use tuple unpacking as a descriptive alternative to indexing

Tuple unpacking is also called multiple assignment and it's sometimes called iterable unpacking because you can actually use it with any iterable in Python, not just with tuples.

You'll most often see tuple unpacking used when looping over an iterable of two-item tuples (or maybe an iterable of three-item tuples) but you can actually use tuple unpacking anywhere in your code where you'd like to give descriptive names to the things within a tuple.

Series: Tuple Unpacking

It's tempting to reach for indexes when working with tuples, lists, and other sequences, but if we know the shape of the tuple we're working with, we can unpack it instead.

Tuple unpacking (aka "multiple assignment" or "iterable unpacking") is often underutilized by new Python programmers.

To track your progress on this Python Morsels topic trail, sign in or sign up.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video