[Fixed] ValueError: too many values to unpack (expected 2)

ValueError: too many values to unpack

While programming in python, we can assign values to two or more variables in a single statement. While doing so, you might get the ValueError exception with the message “ValueError: too many values to unpack ”. If you are trying to assign values to two variables at the same time, you will get the message “ValueError: too many values to unpack (expected 2)”. In this article, we will see why this exception occurs and how we can resolve it.

What is unpacking in python?

When we have a container object like a list, tuple, set, or dictionary, we can assign the elements of the container object directly to variables. Suppose that you have a list containing n elements. You can assign the elements to n variables in a single statement as follows.

This process of assigning the elements of a container object to variables is called unpacking in python. In other words, we unpack the elements from the container and assign them to different variables. You can observe this in the following example.

Output:

In the above example, we have a list named myList having 4 elements in it. In the expression var1, var2, var3, var4 = myList , we have unpacked all the elements of the list into var1,var2,var3, and var4. The elements of the list are assigned to the variables in their respective order. i.e. The first variable gets the first element, the second variable gets the second element, and so on.

What is ValueError: too many values to unpack (expected 2)?

While unpacking, we have to make sure that the number of variables should be equal to the number of elements in the container object. However, if we unpack a container object and assign it to fewer variables than the elements present in the container object, it will raise the ValueError exception with the message ValueError: too many values to unpack. For instance, if we have a list with four elements and we assign it only to three elements, it will raise the ValueError exception as follows.

Output:

You can observe that in the case of a lesser number of variables than the elements in the container object, the program raises the ValueError exception. For instance, we have tried to assign a list with more elements to three variables var1, var2, and var3. This results in an error with the message ValueError: too many values to unpack (expected 3).

If we assign the elements of a list to only two variables and the list happens to contain more than two elements, it will raise the ValueError exception with the message ValueError: too many values to unpack (expected 2) as follows.

Output:

Similarly, if we assign the elements of a tuple, dictionary, or set to two variables and the number of elements is more than 2, the program will raise the ValueError exception with the message ValueError: too many values to unpack (expected 2).

One more situation where this kind of ValueError exception occurs when we work with dictionaries. Generally, beginners think that we can extract keys and values from a dictionary and print them as follows.

However, this will lead to the ValueError exception as follows.

Here, the user thinks that in each iteration of the for loop, a key and a value from myDict will be assigned to the variable key and the variable value. However, this doesn’t happen. In the for loop, myDict works as an iterable object that has the keys of the dictionary in it. In the first execution of the for loop, the string “name” from myDict is assigned to the variables key and value in the for loop. As there are four characters in the string “name“, the unpacking operation fails and the exception is raised with the message ValueError: too many values to unpack (expected 2). Had there been only two characters in the keys of the dictionary, the above for loop will run without any error. However, it won’t produce the desired result. You can observe this in the following example.

Output:

In the above output, you can observe that the variables key and value in the for loop are assigned characters from the strings in the keys of the dictionary. They are not assigned the actual keys and values of the dictionary. Hence, the above code makes no sense, although it executes without any error.

How to resolve ValueError: too many values to unpack (expected 2)?

To avoid the ValueError exception with the message “ValueError: too many values to unpack (expected 2)”, you should make sure that you should use the same number of variables as the elements in the container object while unpacking. For instance, if you have two variables, you should only unpack a container object with only two elements as follows.

Output:

While working with dictionaries, you should use the items() method to iterate through the keys and values of the dictionary as follows.

Output:

Here, the items() method returns a list of tuples. Each tuple contains two elements where the first element is a key of the dictionary and the second element of the tuple is the associated value. Hence, In each iteration of the for loop, a tuple is unpacked and the values are assigned to the variables key and value.

Conclusion

In this article, we discussed why we get the error with the message ValueError: too many values to unpack (expected 2) in python. We also discussed how we can avoid this error in our program. To make sure that you don’t run into this error, you just have to unpack the container objects to the same number of variables as the number of elements in the container object like list, set, or tuple.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!


import_contacts

You may also like:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.