ValueError: too many values to unpack (expected 2) in Python

In Python, “ValueError” arises when we try to access the value that is not present in the given iterable. The value objects are lists, tuple, dicts, set, and strings. When the element is retrieved from any iterable value, then this is referred to as unpacking in Python.

The “too many values to unpack (expected 2)” error appears in Python when we try to unpack too many values from the value. To resolve this error, various solutions are provided by Python.

The following reason and solutions are used to resolve the “too many values to unpack (expected 2)” error in Python:

So, let’s start with the first reason.

Reason 1: Assignment Variables are not Equivalent to Iterable Values

The error invokes in Python when the initialized element of iterable is not equivalent to assignment variables. The below snippet shows this error:

The “ValueError” occurred because the assignment variable “num” and “num1” is not equal to the iterable variable.

Solution: Declare as Many Variables as Items in the Iterable

To rectify this error, please assign equivalent iterable variables and assignment variables in the Python program.

Code:

num, num1, num2, num3 = (10,20,30,40)
print(num, num1, num2, num3)

In the above code, the tuple iterable element is equal to the number of assignment variables. Each iterable value is assigned to its assignment variable.

Output:

The output is displayed in the terminal, which shows the error has been removed.

Reason 2: Iterating Over List or Tuple

Another reason for this error is when the “for” loop iterates over the list or tuple using the index. The for-loop iteration only gets access to the elements of the present iteration.

The above snippet shows an error because there are multiple values to unpack for specific index positions.

Solution: Use enumerate() Function

To rectify this error, the “enumerate()” function is used in Python to convert the iterable tuple or list into an enumerate object.

Code:

list_value = ['Alex', 'Lily', 'David']
tuple_value = ('John', 'Falcon', 'Jelly')
for index, i in enumerate(list_value):
    print(index, i)

print('-----------------------------')

for index, i in enumerate(tuple_value):
    print(index, i)

In the above code, the enumerate() function is used multiple times in a program to accept the tuple and list variable as an argument and returns an enumerated object. The enumerated object can easily return the value according to the index number.

Output:

The above snippet shows the value of each item placed at the specific index position.

Reason 3: Unpacking the Strings

This error also arises when a string value is assigned to multiple variables

The above snippet shows that the string value can not unpack into multiple variables.

Solution: Split the Strings

To resolve this error, we need to split the strings before unpacking the string’s value and assigning it to a variable. To split the string, the “split()” function is used in the below code:

Code:

string_value = 'Python Guide Foss'
z=string_value.split(' ')
val, val1, val2 = z
print(val)
print(val1)
print(val2)

In the above code, the “split()” function takes the space or delimiter as an argument and returns the list of strings by splitting into separate strings. The separated strings are assigned to different variables using the assignment operator.

Output:

The above output shows that the split string values are assigned to each specific variable.

Reason 4: Iterating Over Dictionary

Another reason for this error is when we iterate over the dictionary to get the key and its value using a simple “for” loop.

The above output shows an error because the “for” loop iterates over the dictionary to get the key and its unique value.

Solution: Use dict.items() Function

To resolve this error, we need to use the “dict.items()” function to get the key and its value from the dictionary.

Code:

dict_value = {1: 'Alex', 2: 'Lily', 3: 'John'}
for key, items in dict_value.items():
    print(key, items)

In the above code, the “dict.items()” function is used to return the keys and their value from the input dictionary.

Output:

The above output shows the key name and the value stored in a key at the iteration time.

These are all the reasons and solutions for Python’s “ValueError: too many values to unpack” error.

Conclusion

The “too many values to unpack (expected 2)” error occurs for various reasons, such as assignment variables not equivalent to iterable values, iterating over a list or tuples and strings, etc. To resolve this error in Python, declare as many variables as items in the iterable, use enumerate() and items() functions, etc. This post has presented a detailed guide on resolving Python’s “too many values to unpack (expected 2)” error.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×