ValueError: too many values to unpack (expected 2) means you wrote two names on the left of an assignment, like a, b = something, but something produced more than two items. The most common causes are str.split() returning extra parts, looping over a dict without .items(), and a function that returns more values than you unpack. Match the number of names to the data, or collect the extras with a starred name such as a, b, *rest = something.
Its twin, not enough values to unpack (expected 2, got 1), is the opposite problem: fewer items than names. Both come from the same feature, iterable unpacking, so the fixes overlap.
Every traceback below comes from a real run with Python 3.13.5 on my machine. I also ran the key cases on Python 3.10 and 3.14.8, because 3.14 now tells you how many values it actually got in many cases.
In this guide:
- What unpacking does and why it fails
- Cause 1: More values than names (use a starred name)
- Cause 2: str.split() returned more parts than expected
- Cause 3: Looping over a dict without .items()
- The sneaky version: two-letter keys do not fail
- Cause 4: One row in your data has an extra column
- Cause 5: enumerate() with the wrong number of names
- Cause 6: A function returns more values than you unpack
- Cause 7: Unpacking a string by mistake
- Cause 8: A generator that yields too many items
- Cause 9: not enough values to unpack (expected 2, got 1)
- Cause 10: cannot unpack non-iterable int object
- Python 3.14 tells you how many values it got
- Quick fix table
- Frequently asked questions
What unpacking does and why it fails
When you write x, y = point, Python iterates over point and assigns the first item to x and the second to y. The assignment statement reference says the iterable must produce exactly as many items as there are targets, unless one target is starred. Any mismatch raises ValueError.
Here is the simplest version, three values into two names:
point = [10, 20, 30]
x, y = point
Traceback (most recent call last):
File "c01_too_many.py", line 2, in <module>
x, y = point
^^^^
ValueError: too many values to unpack (expected 2)
Python 3.11 and later underline the left side with ^^^^, which tells you which assignment failed when a line has several.
Cause 1: More values than names (use a starred name)
If you only care about the first few items, collect the rest with *. This is extended unpacking, introduced by PEP 3132:
point = [10, 20, 30]
x, y, *rest = point
print(x, y, rest)
first, *middle, last = [1, 2, 3, 4, 5]
print(first, middle, last)
name, *_ = ["Ana", "ana@example.com", "London"]
print(name)
10 20 [30]
1 [2, 3, 4] 5
Ana
The starred name always becomes a list, even when it holds one item or none. *_ is the convention for "ignore the rest". You can have only one starred name per assignment.
Cause 2: str.split() returned more parts than expected
Splitting a CSV-style line into two names works until the data has an extra field:
line = "Ana,ana@example.com,London"
name, email = line.split(",")
Traceback (most recent call last):
File "c03_split.py", line 2, in <module>
name, email = line.split(",")
^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
You have three good options, depending on what you want. Unpack all the fields, limit the split with maxsplit, or use partition(), which always returns exactly three parts:
line = "Ana,ana@example.com,London"
name, email, city = line.split(",")
print(name, email, city)
name, rest = line.split(",", 1)
print(name, "|", rest)
key, sep, value = "timeout=30=seconds".partition("=")
print(key, value)
key, _, value = "no-equals-here".partition("=")
print(repr(key), repr(value))
Ana ana@example.com London
Ana | ana@example.com,London
timeout 30=seconds
'no-equals-here' ''
The last line shows why partition() is so handy: when the separator is missing, it still returns three values, with empty strings, so the unpacking cannot fail. The string methods reference documents both split(sep, maxsplit) and partition(). For real CSV files with quoted commas, use the csv module; my guide to csv.DictReader and DictWriter shows how.
Cause 3: Looping over a dict without .items()
Iterating a dict gives you its keys only. Each key is a string, and Python tries to unpack that string into two names:
ages = {"ana": 31, "ben": 27}
for name, age in ages:
print(name, age)
Traceback (most recent call last):
File "c04_dict_loop.py", line 2, in <module>
for name, age in ages:
^^^^^^^^^
ValueError: too many values to unpack (expected 2)
"ana" has three characters, so it fails with "expected 2". Add .items() to get key and value pairs:
ages = {"ana": 31, "ben": 27}
for name, age in ages.items():
print(name, age)
ana 31
ben 27
The Python tutorial on looping techniques uses exactly this .items() pattern.
The sneaky version: two-letter keys do not fail
The same bug can run without any error. When every key has exactly two characters, each one unpacks into two letters:
codes = {"uk": "United Kingdom", "us": "United States"}
for code, country in codes:
print(code, country)
u k
u s
The code printed u k instead of uk United Kingdom. Country codes, language codes and two-letter state codes all hit this. If you loop over a dict with two names, .items() belongs there almost every time.
Cause 4: One row in your data has an extra column
Loops over a list of tuples fail as soon as one row has a different length. Notice that the first row printed fine:
rows = [("Ana", 31), ("Ben", 27, "admin")]
for name, age in rows:
print(name, age)
Ana 31
Traceback (most recent call last):
File "c06_rows.py", line 2, in <module>
for name, age in rows:
^^^^^^^^^
ValueError: too many values to unpack (expected 2)
This is typical for data from files, APIs or database queries where one record has an extra field. Check the length and skip or log bad rows:
rows = [("Ana", 31), ("Ben", 27, "admin"), ("Cy",)]
for row in rows:
if len(row) != 2:
print("skipping bad row:", row)
continue
name, age = row
print(name, age)
Ana 31
skipping bad row: ('Ben', 27, 'admin')
skipping bad row: ('Cy',)
If rows can have different valid shapes, structural pattern matching handles each shape in one place, as the pattern matching tutorial in PEP 636 shows:
def describe(row):
match row:
case (name, age):
return f"{name} is {age}"
case (name, age, role):
return f"{name} is {age} ({role})"
case _:
return f"unexpected row: {row!r}"
for r in [("Ana", 31), ("Ben", 27, "admin"), ("Cy",)]:
print(describe(r))
Ana is 31
Ben is 27 (admin)
unexpected row: ('Cy',)
Cause 5: enumerate() with the wrong number of names
enumerate() yields pairs of (index, item). When the item is itself a pair, writing three flat names fails with the opposite message:
pairs = [("a", 1), ("b", 2)]
for i, letter, number in enumerate(pairs):
print(i, letter, number)
Traceback (most recent call last):
File "c07_enumerate.py", line 2, in <module>
for i, letter, number in enumerate(pairs):
^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 3, got 2)
Group the inner names in parentheses so they match the nested structure:
pairs = [("a", 1), ("b", 2)]
for i, (letter, number) in enumerate(pairs, start=1):
print(i, letter, number)
1 a 1
2 b 2
I also used start=1 there, which my post on enumerate starting at 1 covers in more detail.
Cause 6: A function returns more values than you unpack
return a, b, c returns one tuple with three items. Unpacking it into two names fails:
def get_user():
return "Ana", "ana@example.com", 31
name, email = get_user()
Traceback (most recent call last):
File "c08_function_return.py", line 4, in <module>
name, email = get_user()
^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
Unpack all three, use name, email, *_ = get_user(), or keep the tuple in one variable. This often appears after someone adds a value to the return line and misses one of the callers. Returning a dataclass or NamedTuple instead avoids the problem, because callers use attribute names rather than positions. My comparison of list vs tuple vs set vs dict explains why tuples are the natural return type here.
Cause 7: Unpacking a string by mistake
A string is iterable, so Python unpacks it character by character:
first, last = "Ana Silva"
Traceback (most recent call last):
File "c10_string.py", line 1, in <module>
first, last = "Ana Silva"
^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
Split it first; the string methods reference explains how split() with no argument handles runs of spaces. When the number of words can vary, a starred name catches the rest:
first, last = "Ana Silva".split()
print(first, last)
first, *last = "Ana Maria Silva".split()
print(first, last)
Ana Silva
Ana ['Maria', 'Silva']
Cause 8: A generator that yields too many items
Generators, map() objects and file objects unpack the same way as lists:
def numbers():
yield 1
yield 2
yield 3
a, b = numbers()
Traceback (most recent call last):
File "c11_generator.py", line 6, in <module>
a, b = numbers()
^^^^
ValueError: too many values to unpack (expected 2)
Python stops reading after it sees one item too many, so it cannot tell you the full count for a generator. Use a, b, *rest = numbers() if you want to keep going, but remember that the starred name reads the whole generator into a list. For other quick developer checks, see the free tools page.
Cause 9: not enough values to unpack (expected 2, got 1)
The reverse error happens when the data is shorter than the names:
name, email = ["Ana"]
Traceback (most recent call last):
File "c02_not_enough.py", line 1, in <module>
name, email = ["Ana"]
^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
Look for empty fields, a split() on a line without the separator, or a short row. partition() from cause 2 and the length check from cause 4 fix this one too. Python always reports "got N" for this error, because it has already run out of items when it fails.
Cause 10: cannot unpack non-iterable int object
If the right side is a single number or None, the error changes to a TypeError:
def count_items():
return 3
total, label = count_items()
Traceback (most recent call last):
File "c09_non_iterable.py", line 4, in <module>
total, label = count_items()
^^^^^^^^^^^^
TypeError: cannot unpack non-iterable int object
The function returned one value, not two. You see cannot unpack non-iterable NoneType object when a function falls off the end without a return. My post on NoneType object is not subscriptable covers that missing-return pattern in depth.
Python 3.14 tells you how many values it got
I ran the same scripts on Python 3.10 and Python 3.14.8. On 3.10, every case gives the classic message:
-- c01_too_many.py --
ValueError: too many values to unpack (expected 2)
-- c03_split.py --
ValueError: too many values to unpack (expected 2)
-- c08_function_return.py --
ValueError: too many values to unpack (expected 2)
-- c04_dict_loop.py --
ValueError: too many values to unpack (expected 2)
-- c10_string.py --
ValueError: too many values to unpack (expected 2)
-- c11_generator.py --
ValueError: too many values to unpack (expected 2)
On 3.14, unpacking a list or tuple adds the actual count:
-- c01_too_many.py --
ValueError: too many values to unpack (expected 2, got 3)
-- c03_split.py --
ValueError: too many values to unpack (expected 2, got 3)
-- c08_function_return.py --
ValueError: too many values to unpack (expected 2, got 3)
-- c04_dict_loop.py --
ValueError: too many values to unpack (expected 2)
-- c10_string.py --
ValueError: too many values to unpack (expected 2)
-- c11_generator.py --
ValueError: too many values to unpack (expected 2)
The list, the split() result and the returned tuple now say got 3. The dict loop and the string still use the short message, because the item being unpacked there is a string, and the generator cannot report a count at all. The What's New in Python 3.14 page covers the release's other error message improvements; the extra count here is what I saw in my own runs, so match on the start of the message if you parse it in tests.
Quick fix table
Find the line pattern that matches your traceback:
| Line that fails | Why | Fix |
|---|
x, y = [10, 20, 30] | Three values, two names | x, y, *rest = point |
name, email = line.split(",") | The line has more commas than you expected | name, rest = line.split(",", 1) |
for k, v in ages: | Looping over a dict gives keys only | for k, v in ages.items(): |
for name, age in rows: | One row has an extra column | Check len(row) or use match |
for i, a, b in enumerate(pairs): | enumerate gives (index, item) | for i, (a, b) in enumerate(pairs): |
name, email = get_user() | The function returns three values | name, email, age = get_user() |
first, last = "Ana Silva" | A string unpacks into characters | first, last = "Ana Silva".split() |
total, label = count_items() | The function returns one int | total = count_items() |
When you are not sure what the right side holds, print it just before the failing line with print(repr(value), len(value)). The repr() output shows hidden spaces, extra separators and nested tuples that are easy to miss. Python's built-in functions page documents both repr() and enumerate().
Frequently asked questions
What does too many values to unpack (expected 2) mean?
You wrote two names on the left of an assignment or for loop, but the value on the right produced more than two items. Python needs exactly one item per name unless you use a starred name.
How do I fix too many values to unpack in a for loop over a dict?
Add .items(): for key, value in my_dict.items(). Looping over a dict directly gives only the keys, and Python then tries to unpack each key string into two names.
How do I ignore extra values when unpacking?
Use a starred name: first, second, *rest = values, or first, *_ = values. The starred name collects the remaining items into a list.
What is the difference between too many and not enough values to unpack?
Too many means the right side has more items than names. Not enough means it has fewer. Both are ValueError, and the fix is to match the names to the data.
Why does split() cause too many values to unpack?
The string contained the separator more often than you expected, so split() returned extra parts. Use split(sep, 1) to split only once, or partition() for exactly three parts.
Why does my dict loop print single letters?
When every key has two characters, for a, b in my_dict unpacks each key into two letters without an error. Use my_dict.items() to get keys and values.
What does cannot unpack non-iterable int object mean?
The right side is a single int, not a sequence. Usually a function returned one value instead of a tuple. Check the return statement or unpack into one name.
Last checked: 9 November 2026. Figures, settings and guidance can change, so confirm anything important with the sources linked above.