[Solved] Python ValueError: too many values to unpack (Expected X)


In Python, When bags are not unpack well then it throws exception as “ValueError: Too many values to unpack (Expected X) . This exception generally occurs in two cases:

  • When you try to iterate over a dictionary and unpack its keys and values separately.
  • When you forget to unpack every item from a list to a variable.

If you noticed this is ValueError and it’s occurred when you try to access value that doesn’t exists. Values can be any type like object, string, list or dictionary.

Example 1

In this below dictionary iteration example, print values of key and value from user dictionary but it will through exception as “ValueError: Too many values to unpack (expected 2)“. Its’ because loop is try to access two values key and value from dictionary while there is only value because dictionary consider each as value.

user = {
	"name": "Saurabh Gupta",
	"age": 35,
	"city": "Noida"
}
#iterate user dictionary
for key, value in user:
	print("Key:", key)
	print("Value:", str(value))

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 13, in
for key, value in user:

ValueError: too many values to unpack (expected 2)

Solution

To resolve above problem and access key and value from dictionary each item , you can use dictionary items() method which analyze keys and values from dictionary and return these values in iteration. You can modify the code as below:

user = {
	"name": "Saurabh Gupta",
	"age": 35,
	"city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
	print("Key:", key)
	print("Value:", str(value))

Output

After adding items() method in code you can observe below output which is returning key and value of dictionary.

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Note: Till Python 2.0 use method iteritems() in Python 3+ it’s replace with method items().

Example 2

In Python, allows sequence of values to assign on number of variables. In case number of values are more then variables, it throws exception as “ValueError: too many values to unpack (expected X)

grapes, bananas, apples, papaya = [250, 80, 200, 300, 500]

Output

It will throws exception as below because count of variables are less then the values on list.

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 11, in
grapes, bananas, apples, papaya = [250, 80, 200, 300, 500]

ValueError: too many values to unpack (expected 4)

In case number of values are less then variables, it throws exception as “ValueError: not enough values to unpack (expected X)”

grapes, bananas, apples, papaya = [250, 80, 200]

Output :

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 18, in
grapes, bananas, apples, papaya = [250, 80, 200]

ValueError: not enough values to unpack (expected 4, got 3)

This is because of number of values are less (3) while number of values

To resolve such type of issues, always make sure the number of variables should be equal to number of values in list.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s