How to unpack a list in Python?

Python – Unpack a List

In this tutorial, you will learn what unpacking a list means, and how to unpack a list into variables in Python.

Python - Unpack a List
Unpack a list of five items to five variables

What does unpacking a list means

Let us first look into the situation where we need to unpack a list.

Consider a list that has five items as shown below.

nums = [100, 200, 300, 400, 500]

If we want to access the items of this list, we can simply use index.

For example, to access the first item we can use nums[0], and to access the second item we can use nums[1], and so on.

But, what if want to store each of these values into individual variables.

If we use index notation, and assign each of the values in the list to a separate variable, it would look something like this.

num_1 = nums[0]
num_2 = nums[1]
num_3 = nums[2]
num_4 = nums[3]
num_5 = nums[4]

What if I say that there is another way, a more concise way, of doing all these assignments of list items to variables within a single statement.

Yes, there is a technique called unpacking.

In the following statement, we shall unpack the list items to individual variables.

num_1, num_2, num_3, num_4, num_5 = nums

Yes! That’s it. The items in the list are assigned to the variables in the respective order. Let us write a program, and verify the same.

Python Program

nums = [100, 200, 300, 400, 500]

# List unpacking
num_1, num_2, num_3, num_4, num_5 = nums

print(f"num_1 is {num_1}")
print(f"num_2 is {num_2}")
print(f"num_3 is {num_3}")
print(f"num_4 is {num_4}")
print(f"num_5 is {num_5}")
Run Code Copy

Output

num_1 is 100
num_2 is 200
num_3 is 300
num_4 is 400
num_5 is 500

Also, please note that we have to specify as many variables as that of items in the list. For example, in our list, there are five items. Therefore, we have to use five variables while unpacking the list.

Errors while unpacking a list in Python, and how to handle them

If we do not specify as many number of variables as that of values in the list, we get ValueError exception. This can occur in two ways.

  • When the number of variables is less than the length of given list.
  • When the number of variables is greater than the length of given list.

1. ValueError: too many values to unpack

If we use lesser number of variables while unpacking a list, we would get ValueError exception as shown in the following.

In the following program, we tried unpacking a list of five items into three variables.

Python Program

nums = [100, 200, 300, 400, 500]

num_1, num_2, num_3 = nums

print(f"num_1 is {num_1}")
print(f"num_2 is {num_2}")
print(f"num_3 is {num_3}")
Run Code Copy

Output

Traceback (most recent call last):
  File "/Users/pythonexamplesorg/main.py", line 3, in <module>
    num_1, num_2, num_3 = nums
    ^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 3)

We get ValueError exception. And the message too many values to unpack is clear, that there are too many values in the list to unpack into the expected 3 variables.

Solution

If you are interested in unpacking only the first three items in the list to three variables, you have to first unpack the starting required number of items into variables, and then assign the rest into a new list using asterisk symbol as shown in the following statement.

num_1, num_2, num_3, *others = nums

Let us use this statement to unpack the list into fewer number of variables, and run the program.

Python Program

nums = [100, 200, 300, 400, 500]

num_1, num_2, num_3, *others = nums

print(f"num_1 is {num_1}")
print(f"num_2 is {num_2}")
print(f"num_3 is {num_3}")
Run Code Copy

Output

num_1 is 100
num_2 is 200
num_3 is 300

2. ValueError: not enough values to unpack

If we try to unpack the list into more number of variables than the number of items in the list, we get ValueError exception. Here is an example, where we try to unpack our list of five items into seven variables.

Python Program

nums = [100, 200, 300, 400, 500]

num_1, num_2, num_3, num_4, num_5, num_6, num_7 = nums

print(f"num_1 is {num_1}")
print(f"num_2 is {num_2}")
print(f"num_3 is {num_3}")
Run Code Copy

Output

Traceback (most recent call last):
  File "/Users/pythonexamplesorg/main.py", line 3, in <module>
    num_1, num_2, num_3, num_4, num_5, num_6, num_7 = nums
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 7, got 5)

And here as well, the message not enough values to unpack is clear that there are not enough values in the list to unpack into variables. While the list got 5 items, unpacking is expecting 7, because we have given seven variables.

Solution

To rectify this issue, you have to make sure that there are as many number of items in the list as there are variables.

Summary

In this tutorial of Python Lists, we have seen how to unpack a list into variables in Python, with examples.

Related Tutorials

Code copied to clipboard successfully 👍