Python raises ValueError when a function receives an argument with a correct type but an invalid value. Python valueerror: too many values to unpack (expected 3) means the number of variables does not match the number of values you want to unpack.
You can solve the error by ensuring the number of variables matches the number of values you want to unpack. For example,
a, b, c, d = [1, 2, 3, 4] print(a) print(b) print(c) print(d)
In this tutorial, we will go through what unpacking is, examples of the error and how to solve it.
Table of contents
Python ValueError: too many values to unpack (expected 3)
Python functions can return more than one variable. We store these returned values in variables and raise the valueerror when there are not enough objects returned to assign to the variables.
What is Unpacking?
Unpacking refers to assigning individual elements of an iterable to multiple variables simultaneously.
Let’s look at an example of unpacking a list:
# Unpacking a list and assign to three variables d, e, f = [4, 5, 6] print(d) print(e) print(f)
4 5 6
Example #1: Unpacking a List
If the number of variables does not match the values we want to unpack, and the Python interpreter will raise a ValueError. For example,
# Trying to assign four values to three variables a, b, c = [1, 2, 3, 4] print(a) print(b) print(c)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [3], in <cell line: 3>() 1 # Trying to assign three values to two variables ----> 3 a, b, c = [1, 2, 3, 4] 5 print(a) 6 print(b) ValueError: too many values to unpack (expected 3)
The Python interpreter expects three values to assign to the variables a
, b
, and c
but instead has four values.
Solution
To solve this error, we need to ensure the number of variables matches the number of items in the iterable we want to unpack. Let’s look at the revised code:
a, b, c, d = [1, 2, 3, 4] print(a) print(b) print(c) print(d)
Let’s run the code to see the result:
1 2 3 4
We successfully unpacked the list and printed to values to the console.
Example #2: Unpacking a String
This error can also occur when trying to unpack substring values from a string. Let’s look at an example of getting three shape names from a single string.
sq, tri, dia = 'square.triangle.diamond' print(sq) print(tri) print(dia)
Let’s run the code to see what happens.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [5], in <cell line: 1>() ----> 1 sq, tri, dia = 'square.triangle.diamond' 2 print(sq) 3 print(tri) ValueError: too many values to unpack (expected 3)
The error occurs because there are three variables and twenty-three values. When unpacking a string, Python unpacks each character.
Solution
We can solve this error by splitting the string using the full-stop separator to get three strings. We can then assign the three strings to the three variable names.
Let’s look at the revised code:
sq, tri, dia = 'square.triangle.diamond'.split(".") print(sq) print(tri) print(dia)
Let’s run the code to get the result:
square triangle diamond
Example #3: Unpacking a String v2
If we want to unpack the single characters in a string, we need to have the same number of variables as there are characters. Otherwise, we will raise the ValueError:
string = 'defg' d, e, f = string print(d) print(e) print(f)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [8], in <cell line: 3>() 1 string = 'defg' ----> 3 d, e, f = string 5 print(d) 6 print(e) ValueError: too many values to unpack (expected 3)
The error occurs because there are four characters to unpack in the string and only three variables.
Solution
We can solve the error by ensuring there is the same number of variables as there are values to unpack. In this case, we need four variables. Let’s look at the revised code:
string = 'defg' d, e, f, g = string print(d) print(e) print(f) print(g)
Let’s run the code to get the result:
d e f g
We successfully unpacked the characters and printed them to the console.
Example #4: Function returning iterable
We can also encounter this error when we try to unpack the items from the object returned by a function call. Let’s look at an example of a function that returns a tuple containing the surnames of four famous physicists.
def return_tup(): return('Feynman', 'Dirac', 'Boltzmann', 'Curie') fey, di, bol = return_tup() print(fey) print(di) print(bol)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [14], in <cell line: 4>() 1 def return_tup(): 2 return('Feynman', 'Dirac', 'Boltzmann', 'Curie') ----> 4 fey, di, bol = return_tup() 5 print(fey) 6 print(di) ValueError: too many values to unpack (expected 3)
The error occurs because the function returns a tuple containing four items, but we only have three variables.
Solution
We can solve this error by ensuring we have the correct number of variables to match the number of items in the tuple.
Let’s look at the revised code:
def return_tup(): return('Feynman', 'Dirac', 'Boltzmann', 'Curie') fey, di, bol, cur = return_tup() print(fey) print(di) print(bol) print(cur)
Let’s run the code to see the result.
Feynman Dirac Boltzmann Curie
We successfully unpacked the tuple and printed the names to the console.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on ValueErrors involving unpacking, go to the article: How to Solve Python ValueError: too many values to unpack (expected 2)
To learn more about Python for data science and machine learning, go to the online courses page on Python for free and easily accessible courses.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.