← Back

Home › Errors › ValueError

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() 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

  1. Count the names on the left of the = — the message already told you this number as expected N.
  2. Print the right side on the line above: print(parts) and len(parts) — see how many values there really are.
  3. If the extras are real data, add names for them (or star one name to collect them).
  4. If the extras are noise, cut the right side down: slice it (parts[:2]) or cap the split (maxsplit).

Related messages worth knowing

Fix it interactively

These free problems practice controlled unpacking — repair them in the browser:

Practice more Python bugs →
🇮🇳 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 कर लो।

पूरी Hindi explanation पढ़ें →