ValueError: too many values to unpack (tested)

ValueError: too many values to unpack (expected 2) and its twin not enough values to unpack (expected 2, got 1) mean the number of names on the left of = (or in a for) doesn't match the number of items on the right. We measured the usual causes on Python 3.13.5.

1. split() returned a different number of parts

for line in ["ada,36", "bob,41,london", "carl"]:
    try:
        name, age = line.split(",")
        print(name, age)
    except ValueError as e:
        print(repr(line), "->", e)

Output:

ada 36
'bob,41,london' -> too many values to unpack (expected 2)
'carl' -> not enough values to unpack (expected 2, got 1)

Measured on Python 3.13.5: the same line of code gives too many values to unpack (expected 2) when a row has an extra field and not enough values to unpack (expected 2, got 1) when it has fewer. With real data, print the offending line; for CSV use the csv module, which handles quoted commas.

2. Looping over a dict without .items()

d = {"a": 1, "b": 2}
try:
    for k, v in d:                       # iterates keys: "a" is 1 char -> not enough
        print(k, v)
except ValueError as e:
    print("for k, v in d:", e)
d2 = {"ab": 1, "cde": 2}
try:
    for k, v in d2:                      # "ab" unpacks into 2 chars, silently!
        print("k, v =", k, v)
except ValueError as e:
    print("for k, v in d2:", e)
for k, v in d2.items():
    print("items():", k, v)

Output:

for k, v in d: not enough values to unpack (expected 2, got 1)
k, v = a b
for k, v in d2: too many values to unpack (expected 2)
items(): ab 1
items(): cde 2

Measured: for k, v in d iterates the keys and tries to unpack each key string. One-character keys give "not enough values", longer keys "too many", and a two-character key unpacks silently into its two letters (k, v = a b above). Use d.items().

3. Fixes: match the count, or use *rest, partition, maxsplit

def stats(xs):
    return min(xs), max(xs), sum(xs) / len(xs)
try:
    lo, hi = stats([1, 2, 3])
except ValueError as e:
    print(e)
lo, hi, _ = stats([1, 2, 3])
first, *rest = "ada,36,london".split(",")
print(lo, hi, first, rest)
name, age, *_ = "bob,41,london,uk".split(",")
print(name, age)
key, _, value = "a=b=c".partition("=")
print(key, value)
k2, v2 = "a=b=c".split("=", 1)
print(k2, v2)

Output:

too many values to unpack (expected 2)
1 3 ada ['36', 'london']
bob 41
a b=c
a b=c

A function returning three values needs three names (use _ for one you don't need). first, *rest = ... takes whatever is left as a list; name, age, *_ = ... ignores extras. For key/value strings, str.partition("=") always returns three parts, and split("=", 1) splits at most once.

4. Unpacking at the wrong level

points = [(1, 2), (3, 4)]
try:
    x, y = points                         # two tuples -> works but wrong level
    print("x =", x, "y =", y)
    x, y = [(1, 2), (3, 4), (5, 6)]
except ValueError as e:
    print(e)
for x, y in points:                       # unpack each item instead
    print(x, y)
for i, (x, y) in enumerate(points):
    print(i, x, y)

Output:

x = (1, 2) y = (3, 4)
too many values to unpack (expected 2)
1 2
3 4
0 1 2
1 3 4

Measured: x, y = points unpacks the list, not each point: with two points it "works" and gives two tuples, with three it fails. Unpack inside the loop, and use for i, (x, y) in enumerate(points) when you also need the index.

Frequently asked questions

What does ValueError: too many values to unpack (expected 2) mean?

The right side produced more items than there are names on the left. We measured it from split(',') on a row with three fields assigned to two names, and from looping over a dict without .items().

What does not enough values to unpack (expected 2, got 1) mean?

The right side produced fewer items than names, e.g. a line with no comma split into one part. We measured it on Python 3.13.5.

Why does for k, v in dict give too many values to unpack?

Iterating a dict yields keys, and each key string gets unpacked letter by letter. Use dict.items(); we measured a 2-letter key silently unpacking into its letters.

How do I ignore extra values when unpacking?

Use a starred name: first, *rest = ... or a, b, *_ = .... For strings, split(sep, 1) or partition(sep) limit the parts. We measured each.

Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: split() rows with extra and missing fields, looping over a dict with and without .items(), a 3-value return into 2 names, starred unpacking, partition and maxsplit, and unpacking a list of points at the wrong level. Every block was run verbatim and its output pasted from that run. Message wording on other Python versions was not tested. PlainHeaders is our extension; everything else here is built-in Python.