ValueError: too many values to unpack (expected 2)
In the Python tutorial book I’m using, I typed an example given for simultaneous assignment. I get the aforementioned ValueError when I run the program, and can’t figure out why.
Here’s the output.
![]()
4 Answers 4
Judging by the prompt message, you forgot to call str.split at the end of the 8th line:
Doing so splits the input on the comma. See a demonstration below:
The above code will work fine on Python 2.x. Because input behaves as raw_input followed by a eval on Python 2.x as documented here — https://docs.python.org/2/library/functions.html#input
However, above code throws the error you mentioned on Python 3.x. On Python 3.x you can use the ast module’s literal_eval() method on the user input.
This is what I mean:
![]()
This is because behavior of input changed in python3
In python2.7 input returns value and your program work fine in this version
But in python3 input returns string
try this and it will work fine!
![]()
that means your function return more value!
in python2 the function cv2.findContours() return —> contours, hierarchy
but in python3 findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
so when you use those function, contours, hierachy = cv2.findContours(. ) is well by python2, but in python3 function return 3 value to 2 variable.
How to Fix Valueerror: too many values to unpack

In this article, I will explain the main reason why you will encounter the Valueerror too many values to unpack error in python and a few different ways to fix it. I think it is essential to understand why this error occurs, just because if you have a good understanding of what caused it, you will avoid this error in the future and write better code.
This tutorial contains some code examples and possible ways to fix the error.
Table of Contents
Too many values to unpack
Unpacking is quite a powerful feature in python. Unpacking will assign the values on the right-hand side to the variables on the left-hand side. Each of the values is assigned to one of the variables. Unpacking works when the number of variables and the numbers of values is the same. Every value has a corresponding variable.
See below a simple code snippet that will return the error Valueerror too many values to unpack
Check out the line above. There are three values on the right and only two variables on the left. The name Gordon will be left unassigned. The number of values on the right and the number of variables on the left don’t match. You can fix the code just by removing one of the values. See the code below:
For loop example: Too many values to unpack
Another case where the ‘Too many values to unpack’ error may occur when you are looping through dictionary’s entries. See the code snippet below that returns the error:
Why is the code above returning an error? At line 5, the issue is a dictionary used in a loop, by default, will return a list of keys. Unpacking one of the keys, which is one value, into two variables results in an unpacking error.
Solution #1
At the point of unpacking the dictionary entries, the number of values and the number of variables is not matching. One way to solve this is by using the dictionary .items() method. This method returns a list of tuples (key, value), two values for two variables. Problem solved! See the code below:
Solution #2
I think it is a good idea to see several ways to fix the error, to validate your understanding. Since the problem was having two variables and only one value to assign, another possible solution is to avoid unpacking. How can you do that? Just removing the value variable from the loop. See the code example below:
This code does the same as solution #1. It is just a different way to solve the same problem. The first solution is slightly more efficient; just you don’t have to search inside the loop.
Split example: Too many values to unpack
The unpacking error could arise when using the .split() method. The code below is the simplest case where using split returns a valueerror.
Why is this code returning an error? The number of values on the right-hand side of the equal sign and the number of variables on the other side are not matching. The result of executing ‘word1.word2.word3’.split(‘.’) is a list containing three values: [‘word1’, ‘word2’, ‘word3’] .
Solution #1
The best way to avoid the unpacking error, in this case, is avoiding unpacking. The reason is that if you are not sure what the input of the split will be, you can’t be sure the outcome of splitting will always be two items. Therefore the safest option is avoiding unpacking.
How do you avoid unpacking? Just assigning the result to one variable. See the example below:
This code will not raise any error.
Another example
Let’s see another example where you could encounter this unpacking issue. This issue can also arise when using the input() method. See the code example below:
The input() method will receive whatever the user type and save it as a string value. Therefore in the code example above, we are trying to assign one value to two variables. That means unpacking error. How can you solve it?
Solution #1
The safest and most straightforward solution is avoiding unpacking by just using one variable.
Assuming the user enters two numbers separated by white space, this code won’t raise any error.
Solution #2
There is another way to fix the code below; however, this solution is not as safe and predictable as the previous one. Therefore I will encourage you to use the last approach when possible.
Another possible way to fix the code below is calling the .split() method right after receiving the input. That will split the string into a list. However, this approach is not error free. If the user entered more than two numbers, you would end up having the unpacking problem again.
Not enough values to unpack
We have seen the unpacking issue where you have more values than variables. It’s also possible having more variables than values. In that case, you will get a Not enough values to unpack error. The issue is the same, but the other way around, from left to right. See a code example below:
To prevent this problem, make sure the number of values on one side and the numbers of variables on the other side pair up. In this case, I can avoid the error just by removing the variable name4 . See the code below:
Knowledge Quiz Time!
Here is a chance to check how much you learned. See below a few quiz questions that will help you to confirm and reinforce your understanding. Find the solutions at the bottom of this article:
- What would this program output?
A) ValueError: not enough values to unpack
B) orange
C) pear
2. What would this program output?
A) ValueError: too many values to unpack
C) value1 value2 value3
Conclusion
To summarise, we have seen a few cases where the Valueerror: too many values to unpack error could occur and how you can fix it. You could make sure the values and the variables are pairing up. Another way to avoid this issue is by avoiding unpacking. I hope you enjoy this article, and understand this issue better to avoid it when you are programming.
How to Solve Python ValueError: too many values to unpack (expected 2)
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 2) means you are trying to access too many values from an iterator.
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 2)
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 an operation consisting of assigning an iterable of values to a tuple or a list of variables in a single assignment. The complement to unpacking is packing, which means collecting several values in a single variable. In this context, we can use the unpacking operator * to collect or pack multiple variables in a single variable. In the following example, we will pack a tuple of three values into a single variable using the * operator:
The left side of the assignment must be a tuple or a list, which means we need to use a trailing comma. If we do not use a trailing comma, we will raise the SyntaxError: starred assignment target must be in a list or tuple.
Let’s look at the ways of unpacking in Python.
Unpacking using Tuple and List
In Python, we put the tuple of variables on the left side of the assignment operator = and a tuple of values on the right side. The values on the right will be assigned to the variables on the left using their position in the tuple. This operation is generalizable to all kinds of iterables, not just tuples. Unpacking operations are helpful because they make the code more readable.
To create a tuple object, we do not need to use a pair of parentheses as delimiters. Therefore, the following syntaxes are valid:
Let’s look at an example of unpacking a list:
If we use two variables on the left and three values on the right, we will raise a ValueError telling us that there are too many values to unpack:
If we use more variables than values, we will raise a ValueError telling us that there are not enough values to unpack:
Unpacking Using Asterisk
We can use the packing operator * to group variables into a list. Suppose we have a number of variables less than the number of elements in a list. In that case, the packing operator adds the excess elements together as a list and assigns them to the last variable. Let’s look at an example of unpacking using * :
We can use the asterisk when we have a function receiving multiple arguments. If we pass a list to a function that needs multiple arguments without unpacking, we will raise a TypeError.
The function expects three arguments, but num_list is a single argument. To resolve the TypeError, we unpack the list when passing to print_func .
There are three mistakes that we can make that cause the valueerror: too many values to unpack (expected 2):
- Trying to iterate over a dictionary and unpack its keys and values separately
- Not assigning every element in a list to a variable
- Trying to. unpack too many values while using functions
Example #1: Iterating Over a Dictionary
In Python, a dictionary is a set of unordered items stored as key-value pairs. Let’s consider a dictionary called muon_particle , which holds information about the muon. The dictionary consists of three keys, name, mass, and charge. Each key has a respective value, written to the right side of the key and separated by a colon.
We raise the Value error because each item in the muon_particle dictionary is a value, and keys and values are not distinct values in the dictionary.
Solution
We include the items() method in the for loop to solve this error. This method analyzes a dictionary and returns the keys and values as tuples. Let’s make the change and see what happens.
If we print out the contents of muon_particles.items() , we get the key-value pairs stored as tuples:
Example #2: Unpacking a List to A Variable
If we try to unpack a list to a variable and the number of elements in the list are not equal to the number of assigned variables, we will raise the ValueError. Let’s consider a list of length five, but there are only two variables on the left-hand side of the assignment operator:
We have to ensure that the number of variables we want to unpack equals the number of elements in the list.
Solution
We have to make sure there are five variables to unpack the list:
Example #3: Unpacking Too Many Values while using Functions
We can raise the ValueError when calling functions. Let’s look at the input() function, which takes the input from the user, returns a string type value and assigns it to a variable. This example will give the input() function a first and second name as one string. The program will try to assign the input to two variables, first name and last name:
We raise the ValueError because the interpreter expects two values, but we return the input as a single string.
Solution
We can use the split() function to solve this error, which returns a list of substrings from a given string. To learn more about getting substrings from strings, go to the article titled “How to Get a Substring From a String in Python”. We create the substring using the delimiter space ' ' chosen by default. Let’s look at the use of split() to solve the error:
Summary
Congratulations on reading to the end of this tutorial! The ValueError: too many values to unpack (expected 2)” occurs when you do not unpack all of the items in a list.
A common mistake is trying to unpack too many values into variables. We can solve this by ensuring the number of variables equals the number of items in the list to unpack.
The error can also happen when trying to iterate over the items in a dictionary. To solve this, you need to use the items() method to iterate over a dictionary.
If you are using a function that returns a number of values, ensure you are assigning to an equal number of variables.
If you pass a list to a function, and the function expects more than one argument, you can unpack the list using * when passing the list to the function.
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.