ValueError: too many values to unpack (expected 2) — decoded
Exception: ValueErrorCategory: runtimeRaised at: at the = sign, before any name is bound
What this message means
Multiple assignment is a contract: the right side hands out values, the left side holds names, and the two counts must match exactly. When the right side produces more values than there are names, Python has nowhere to put the leftovers and stops *before binding anything* — not even the first name.
expected 2 is the number of names on the left. The sibling message is not enough values to unpack (expected 3, got 1) — the same contract broken in the other direction.
Traceback (most recent call last):
File "main.py", line 1, in <module>
a, b = [1, 2, 3]
^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
A real example
line = "Aarav 21 Indore"
name, age = line.split() # split() gave THREE values
print(name, age)
The city had nowhere to go. Three honest fixes, depending on what the data actually looks like:
name, age, city = line.split() # 1. name every value you know about
name, age, *rest = line.split() # 2. absorb the extras into a list
first, second = line.split()[:2] # 3. keep exactly the first two
The star operator, properly understood
A starred name in an unpacking collects the leftovers into a list — it is the pressure valve for "there may be more":
first, *middle, last = [1, 2, 3, 4, 5]
# first=1, middle=[2, 3, 4], last=5 — star in the middle works too
name, *extras = "Aarav 21 Indore".split()
# name="Aarav", extras=["21", "Indore"]
With the star in place the count never has to match — extra values are caught, and when there are *fewer* values the starred name simply becomes an empty list instead of crashing.
The everyday shapes of this bug
split()on free-form input. Users type extra spaces or extra words; the word count is whatever they typed.split(maxsplit=1)caps it deliberately.- A function grew a return value.
x, y = get_point()was written whenget_pointreturned two things; it now returns three. - Iterating pairs over triples.
for key, value in items:whereitemsholds 3-tuples. - Unpacking a string.
a, b = "hi"works (strings unpack to characters) — which hides the bug until the string is three letters long.
# split() with a cap — at most 2 parts, however many spaces the user types
key, value = "theme dark extra".split(maxsplit=1)
# key="theme", value="dark extra" — everything after the FIRST space stays together
The fix, step by step
- Count the names on the left of the
=— the message already told you this number asexpected N. - Print the right side on the line above:
print(parts)andlen(parts)— see how many values there really are. - If the extras are real data, add names for them (or star one name to collect them).
- If the extras are noise, cut the right side down: slice it (
parts[:2]) or cap the split (maxsplit).
Related messages worth knowing
- ValueError: the parent guide — right type, impossible value — the family explained.
- ValueError: invalid literal for int() — the other famous wording — text that cannot become a number.
- TypeError: int is not iterable — unpacking is iteration — this is the sibling failure when nothing can be handed out.
Fix it interactively
These free problems practice controlled unpacking — repair them in the browser:
- Tuple unpacking with functions medium
- Multiple return values medium
🇮🇳 Hindi में समझें
Multiple assignment एक contract है: बाईं तरफ जितने names, दाईं तरफ उतने ही values — exact match। एक भी value ज़्यादा हुई तो too many values to unpack (expected 2)। सबसे common जगह: a, b = input().split() — user ने एक extra word लिख दिया और program crash। Fix: (1) सारे values के names लिखो, (2) एक name पर star लगाओ — name, *rest = parts — बची हुई values list में collect हो जाएँगी, (3) split(maxsplit=1) से ही cap लगा दो, या (4) parts[:2] slice कर लो।