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

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.