Unpacking

Ignoring Values While Unpacking and the * Operator

When unpacking values from an iterable, you might want to extract only certain values and ignore others or you might want to capture and combine a subset of the values. In this lesson, you'll learn to do so using the _ placeholder and the * operator.


We've learned that unpacking is the process of extracting values from an iterable (like a list or tuple) and assigning them to multiple variables in a single statement, as shown below:

x, y, z = 10, 20, 30
Python

But now imagine we're only interested in the x and the z variable.

In that case, we would have a variable y in our program that is never used.

While this won't throw an error, it could confuse other developers reading your code, as it may not be obvious that y is irrelevant.

To make it clearer that a value should be ignored, it is common practice to assign it to the character _:

x, _, z = (10, 20, 30) print(x, z)
Python
Output

This way, anyone reading your code knows that the middle value should be ignored.

However, keep in mind that this is just a convenient syntax. Under the hood, you are simply creating a new variable called _:

x, _, z = (10, 20, 30) print(_)
Python
Output

It doesn't have to be the middle value; you can choose any value to ignore:

x, y, _ = (10, 20, 30) print(x, y) # outcome: 10 20 _, y, z = (10, 20, 30) print(y, z) # outcome: 20 30 x, _, _ = (10, 20, 30) print(x) # outcome: 10
Python

What will be the output?

_, b, c, d = [1, 2, 3, 4] print(b, c, d)
Python

What will be the output?

a, _, b, _, c = 'coder' print(a, b, c)
Python

What will be the output?

a, b, c, _ = (10, 20, 30) print(a, b, c)
Python

Great job!

Now consider the following tuple containing user data:

data = ('John Doe', 30, 'Engineer', 'NY', 'profile_picture.png')
Python

Let's say we want to extract the name and age from the tuple and aggregate the remaining values into a more general user_info list.

We can achieve this using the * operator:

data = ('John Doe', 30, 'Engineer', 'NY', 'profile_picture.png') name, age, *user_info = data print(f'{name} is {age} years old.') print(user_info)
Python
Output

Here, we have assigned the first two values of data to the variables name and age. The * before the user_info variable instructs Python to pack all the remaining values into a list, which is then assigned to user_info.

The values that are aggregated depend on the number and position of the other variables.

For example, here we extract the name and the profile picture URL individually and assign the aggregated middle values to user_info:

data = ('John Doe', 30, 'Engineer', 'NY', 'profile_picture.png') name, *user_info, picture = data print(name) print(picture) print(user_info)
Python
Output

If you use the asterisk operator when there are no remaining values, it will assign an empty list:

a, *b, c print(a, b, c)
Python
Output

What will be the output?

a, b, *c = 'coder' print(a, b, c)
Python

What will be the output?

*a, b = (1, 2, 3, 4) print(b)
Python

What will be the output?

a, b, c, *d = (1, 2, 3, 4) print(d)
Python

What will be the output?

a, b, *c = 1, 2 print(c)
Python

Great! Last but not least, you can also combine the _ placeholder with the * operator.

This is helpful if you want to ignore multiple values while unpacking.

Consider the following example where we unpack only the first two values of data:

data = (1, 2, 3, 4, 5, 6) first, second, _, _, _, _ = data
Python

This can be done more efficiently by using the * operator.

In this example, we ignore all the remaining values at once without needing to match the exact number of values in data:

data = (1, 2, 3, 4, 5, 6) first, second, *_ = data print(first, second)
Python
Output

What will be the output?

*_, b = (1, 2, 3, 4) print(b)
Python

What will be the output?

*a, _ = (1, 2, 3, 4) print(a)
Python