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.
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
- Count the names and the values: they must be equal.
- Use a starred name to collect the extra values:
a, *rest = 1, 2, 3. - Print what
input().split()really returns to see how many pieces there are.