Tuple unpacking

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.

0%
Watch other topic trails

Transcript

Let's talk about tuple unpacking in Python.

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 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 tuple 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.

Summary

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.