ValueError: need more than 2 values to unpack in Python

During a multiple value assignment, the ValueError: need more than 2 values to unpack occurs when either you have fewer objects to assign than variables, or you have more variables than objects.

Python has a very rich assignment feature. Python can store multiple values for variables in the assignment operator. Python functions can return a number of values to the calling function. The ValueError: need more than 2 values to unpack caused by the mismatch between the number of values returned and the number of variables in the assignment statement.

If the python function returns less objects than the available variables, the python interpreter can not assign the value to the excess variable. We ‘re going to see this value error and how to fix it in this article.

The older version of the python will throw different error “ValueError: not enough values to unpack (expected 3, got 2)



Exceptions

Recent Python version

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    a, b, c, d = "Lion", "Tiger", "Monkey"
ValueError: need more than 3 values to unpack
[Finished in 0.0s with exit code 1]

Older Python version

Traceback (most recent call last):
  File "./test.py", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 3)


Root Cause

Python has an unique feature of returning multiple values in functions as well as assignment operators. The value error is due to either less values being returned than the variables available or not having enough objects for the variables. This error is caused by the mismatch between the number of variables and the number of values.

The total number of values returned must be the same as the number of variables returned. This is going to resolve this value error.



Solution 1

Find the total number of variables and the total number of values for the assignment operator. Add additional values to the assignment operator. The additional value is assigned to the variable. Passing the additional value to the variable will resolve this value error.

Program

a, b, c, d = "Lion", "Tiger", "Monkey"

print(a)
print(b)
print(c)
print(d)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    a, b, c, d = "Lion", "Tiger", "Monkey"
ValueError: need more than 3 values to unpack
[Finished in 0.1s with exit code 1]

Solution

a, b, c, d = "Lion", "Tiger", "Monkey", "Giraffe"

print(a)
print(b)
print(c)
print(d)

Output

Lion
Tiger
Monkey
Giraffe
[Finished in 0.1s]


Solution 2

Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same. This will resolve the value error.

Program

a, b, c, d = "Lion", "Tiger", "Monkey"

print(a)
print(b)
print(c)
print(d)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    a, b, c, d = "Lion", "Tiger", "Monkey"
ValueError: need more than 3 values to unpack
[Finished in 0.1s with exit code 1]

Solution

a, b, c = "Lion", "Tiger", "Monkey"

print(a)
print(b)
print(c)

Output

Lion
Tiger
Monkey
[Finished in 0.0s]



Related Articles

Leave a Reply

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