Categories
Python Answers

How to fix the “Too many values to unpack” Exception in Python?

Spread the love

To fix the "Too many values to unpack" Exception in Python, we should unpack only the number of items listed in the tuple.

For instance, we write

def returnATupleWithThreeValues():
    return (1,2,3)
a,b,c = returnATupleWithThreeValues()
print a
print b
print c

to unpack all 3 items by assigning the tuple returned by returnATupleWithThreeValues to a, b, and c.

But if we write

def returnATupleWithThreeValues():
    return (1,2,3)
a,b = returnATupleWithThreeValues()
print a
print b

then we’ll get the error since we only unpacked 2 of the items and there’re 3 returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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