ValueError: too many values to unpack (expected 2)

Python · updated 2026-11-04

Raised by an assignment that unpacks an iterable into a fixed number of names, such as a, b = value or for key, value in items, when the iterable yields more items than there are names. The "expected" number is how many names were on the left.

Traceback (most recent call last):
  File "/app/import_rates.py", line 18, in load_rates
    for code, rate in rates:
        ^^^^^^^^^^
ValueError: too many values to unpack (expected 2)

Quick fix

Print what is being unpacked. Looping over a dict gives keys only, so for k, v in d unpacks each key string: use d.items(). For split(), limit it with split(',', 1). If the extra items are expected, collect them with a, b, *rest = value.

  1. Log repr() of the value being unpacked and its length right before the failing line.
  2. Dict in a for loop: iterate d.items() to get key and value pairs.
  3. str.split: pass maxsplit (line.split(',', 1)) or use str.partition when a field can contain the separator.
  4. Tuples from a function: check every return statement returns the same number of values.
  5. Variable length on purpose: use star unpacking (first, *rest = row).
python
rates = {"USD": 1.0, "EUR": 0.92}

# Wrong: iterating a dict yields keys, and "USD" unpacks into 3 characters
# for code, rate in rates:

for code, rate in rates.items():
    print(code, rate)

name, value = "timeout=30=s".split("=", 1)   # ('timeout', '30=s')
first, *rest = [1, 2, 3, 4]                 # first=1, rest=[2, 3, 4]

Why it happens

Looping over a dict without .items()

for k, v in d iterates keys. A key that is a string of two characters unpacks without an error, so it can work in tests and fail on a longer key in production.

split() found more separators

a, b = line.split(",") works until a field contains a comma: a CSV value, a URL with a query string, a timestamp with colons.

A function or query returns more columns

Adding a column to a SELECT, or a third value to a return statement, breaks every caller that unpacks into two names.

Rows of uneven length

Unpacking rows from a CSV, an API list or zip() breaks on the first row with an extra field.

Other fixes

  • For CSV data, use csv.reader or csv.DictReader instead of split so quoted commas are handled.
  • Unpack database rows by column name (a dict row factory or ORM attributes) so new columns do not break callers.

Same error, other wording

  • ValueError: too many values to unpack (expected 3)
  • ValueError: not enough values to unpack (expected 2, got 1)
  • TypeError: cannot unpack non-iterable NoneType object

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the "too many values to unpack" errors in production, show me the value being unpacked at that line, and fix the unpacking (items(), maxsplit or star unpacking) for the inputs we get.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

More in Undefined, None and type errors

References: Python tutorial: tuples and sequence unpacking, Python docs: dict.items, Python docs: str.split (maxsplit), PEP 3132: extended iterable unpacking