ValueError: too many values to unpack
Updated
The Python error "too many values to unpack (expected 2)" means the right side has more items than names on the left. The usual case is a loop over a dict.
What you see
config = {"host": "localhost", "port": 8080}
for key, value in config:
print(key, value)Traceback (most recent call last):
File "/home/me/project/main.py", line 3, in <module>
for key, value in config:
^^^^^^^^^^
ValueError: too many values to unpack (expected 2)What it means
key, value = something takes something apart into exactly two pieces. Here each something is one key of the dict, the string "host", which has four characters, so there are four pieces for two names.
Looping over a dict gives its keys, not its key and value pairs. That is the most common way to meet this error.
Why it happens
- Looping over a dict with two names, as in the example.
- A
split()that found more parts than expected, as inname, email = "Ada, Lovelace, ada@example.com".split(","). - A function that returns more values than the caller unpacks.
- Rows with more columns than names, from a CSV file or a database query.
How to fix it
For a dict, ask for the pairs with .items():
for key, value in config.items():
print(key, value)
For text, cap the number of splits, or collect the rest with a starred name:
name, rest = line.split(",", 1) # at most two parts
first, *others = line.split(",") # first part, then a list of the rest
Example: a tuple with one item too many
point = (3, 4, 5)
x, y = pointTraceback (most recent call last):
File "/home/me/project/main.py", line 2, in <module>
x, y = point
^^^^
ValueError: too many values to unpack (expected 2, got 3)| Python | Prints |
|---|---|
| 3.9–3.13 | ValueError: too many values to unpack (expected 2) |
| 3.14–3.15 | ValueError: too many values to unpack (expected 2, got 3) |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
How each Python version words it
| Python | Prints |
|---|---|
| 3.9–3.15 | ValueError: too many values to unpack (expected 2) |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
Python 3.14 adds how many values it got, "expected 2, got 3", when the right side is a tuple, a list or a dict. For a string, a range or an iterator it still says only "expected 2", which is why the dict loop above prints no count: each item it unpacks is a key string.
Questions
Why does it say "expected 2" in a for loop?
Because the loop target key, value has two names. Python tries to take each item apart into two, and each item of a dict is one key: "host" comes apart into four letters.
How do I unpack when the length varies?
Use a starred name, which takes whatever is left as a list: head, *tail = items works for any non-empty items.
Sources
- Assignment statements, Python language reference
- PEP 3132: extended iterable unpacking, Python Enhancement Proposals
- Mapping types:
dict.items(), Python documentation
Read on October 25, 2026.