Write Python. Understand every error.

Open the editor

Error guide · ValueError

ValueError: too many values to unpack (expected 2)

You tried to split a group of values into fewer names than there are values.

Example

a, b = 1, 2, 3

What Python says

ValueError: too many values to unpack (expected 2)

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

Unpacking puts each value into its own name, so the number of names must match the number of values. Here there are three values and two names. This often happens with a, b = input().split() when the user types three words.

How to fix it

a, b, c = 1, 2, 3
print(a, b, c)

Output: 1 2 3

Tips

Learn the ideas behind it

Other common errors

Share this page: