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.
- Log repr() of the value being unpacked and its length right before the failing line.
- Dict in a for loop: iterate d.items() to get key and value pairs.
- str.split: pass maxsplit (line.split(',', 1)) or use str.partition when a field can contain the separator.
- Tuples from a function: check every return statement returns the same number of values.
- Variable length on purpose: use star unpacking (first, *rest = row).
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.
Related errors
More in Undefined, None and type errors
- TypeError: Cannot read properties of undefined (reading 'x')Node.js
- TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.Node.js
- TypeError: items is not iterableNode.js
- TypeError: x is not a functionNode.js
- ReferenceError: x is not definedNode.js
- ERR_INVALID_ARG_TYPE: The "path" argument must be of type string. Received undefinedNode.js
- TypeError: 'NoneType' object is not subscriptablePython
- AttributeError: 'NoneType' object has no attribute 'x'Python
- TypeError: unsupported operand type(s) for +: 'int' and 'str'Python
- ValueError: invalid literal for int() with base 10: ''Python
- TypeError: __init__() got an unexpected keyword argument 'proxies'Python
- UnboundLocalError: cannot access local variable 'total' where it is not associated with a...Python
- RuntimeError: dictionary changed size during iterationPython
- TypeError: 'str' object is not callablePython
References: Python tutorial: tuples and sequence unpacking, Python docs: dict.items, Python docs: str.split (maxsplit), PEP 3132: extended iterable unpacking