ValueError: too many values to unpack (Fix)

ValueError: too many values to unpack happens when Python tries to assign more values than your variables can hold.

This usually appears when:

  • you unpack a list or tuple into too few variables
  • a for loop expects the wrong number of values
  • a function returns more values than expected
  • split() gives you more parts than you planned for

The fix is usually simple: make the number of variables match the number of values, or use starred unpacking to collect extras.

Quick fix #

values = [1, 2, 3]
a, b, c = values  # number of variables matches number of items

pairs = [("a", 1), ("b", 2)]
for key, value in pairs:
    print(key, value)

Match the number of variables to the number of values. In loops, also check the shape of each item you are unpacking.

What this error means #

Python lets you unpack values into separate variables.

For example:

x, y = (10, 20)
print(x)
print(y)

Output:

10
20

Here, the tuple has 2 items, and there are 2 variables.

The error happens when the value on the right side has more items than the variables on the left side.

A common example is trying to unpack 3 items into 2 variables.

A simple example that causes the error #

values = [1, 2, 3]
a, b = values

This raises:

ValueError: too many values to unpack (expected 2)
Traceback (most recent call last):File "example.py", line 2, in <module>a, b = valuesValueError: too many values to unpack (expected 2)Where it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Read it bottom-up: two variables expected two values, but the list had three.

Why?

  • values contains 3 items
  • a, b only provides 2 variables
  • Python does not know what to do with the extra item

To fix it, either use 3 variables:

values = [1, 2, 3]
a, b, c = values

print(a, b, c)

Output:

1 2 3

Why it happens #

This error usually means your code expected a different shape of data.

Common causes include:

  • The value on the right side has more items than expected.
  • A loop is unpacking each item incorrectly.
  • A function returns more values than your code expects.
  • A string split produces more parts than your variables can hold.

For example, if you are learning how functions return multiple values, see Python functions explained.

Fix 1: Use the correct number of variables #

If you know exactly how many values you will get, use the same number of variables.

Example with a tuple #

point = (5, 10, 15)
x, y, z = point

print(x)
print(y)
print(z)

Output:

5
10
15

Example with a function return value #

def get_user():
    return "Sam", 25, "admin"

name, age, role = get_user()
print(name, age, role)

Output:

Sam 25 admin

This is the best fix when:

  • the number of values is fixed
  • you know exactly what each value means
  • you want clear variable names

Fix 2: Use starred unpacking #

Sometimes you only care about the first or last values, and the number of extra items may change.

In that case, use starred unpacking with *.

data = [10, 20, 30, 40, 50]

first, *middle, last = data

print(first)
print(middle)
print(last)

Output:

10
[20, 30, 40]
50

The *middle part collects all extra values into a list.

This is helpful when:

  • the number of extra items is not fixed
  • you want to ignore or collect the remaining values
  • you only need part of the sequence

Another simple example:

values = [1, 2, 3]
a, *rest = values

print(a)
print(rest)

Output:

1
[2, 3]

If you want to understand unpacking more generally, it can help to review Python statements and expressions.

Fix 3: Check loops that unpack items #

A very common cause is a loop like this:

data = [(1, 2, 3), (4, 5, 6)]

for a, b in data:
    print(a, b)

This fails because each item has 3 values, but the loop expects 2.

You can fix it by matching the shape of each item:

data = [(1, 2, 3), (4, 5, 6)]

for a, b, c in data:
    print(a, b, c)

Output:

1 2 3
4 5 6

Watch out for mixed data #

If items have different lengths, unpacking can fail partway through the loop.

data = [(1, 2), (3, 4, 5)]

for a, b in data:
    print(a, b)

The first item works, but the second item has 3 values, so Python raises the error there.

This also happens often when looping through dictionaries incorrectly. If you need help with that, see how to loop through a dictionary in Python.

Fix 4: Check split() results #

String splitting is another common source of this error.

text = "apple,banana,cherry"
first, second = text.split(",")

This raises the error because split(",") returns 3 parts:

['apple', 'banana', 'cherry']

Fix by using enough variables #

text = "apple,banana,cherry"
first, second, third = text.split(",")

print(first)
print(second)
print(third)

Fix by limiting the split #

If you only want 2 parts, use maxsplit:

text = "apple,banana,cherry"
first, rest = text.split(",", maxsplit=1)

print(first)
print(rest)

Output:

apple
banana,cherry

If you want more detail on how this method works, see the Python string.split() method.

Debugging steps #

When you see this error, the fastest way to fix it is to inspect the value before unpacking.

Useful checks:

print(value)
print(type(value))
print(len(value))

If the problem happens in a loop:

for item in data:
    print(item, len(item))

If the problem involves string splitting:

print(text.split(','))

What to look for #

  • Print the value before unpacking it.
  • Use len() when the value is a list, tuple, or other sequence.
  • Check what each loop item looks like.
  • Read the full traceback and find the exact line where unpacking happens.

For example:

data = [("a", 1), ("b", 2, 3)]

for item in data:
    print(item, len(item))

Output:

('a', 1) 2
('b', 2, 3) 3

Now the problem is clear: not every item has the same number of values.

Do not confuse this error with these similar ones:

Common causes #

Here are the most common situations that trigger this error:

  • Unpacking a tuple or list into too few variables
  • Using for a, b in ... when items contain 3 or more values
  • Splitting a string into more parts than expected
  • Expecting a function to return 2 values when it returns 3
  • Unpacking dictionary iteration incorrectly

FAQ #

What does “unpack” mean in Python? #

It means assigning items from a sequence like a list or tuple into separate variables.

Why do I get this error in a for loop? #

Each item in the loop likely has more values than the variables in the loop header.

How do I ignore extra values? #

Use starred unpacking such as first, *rest = values.

Is this the same as “not enough values to unpack”? #

No. That error means you have fewer values than variables. This one means you have more values than variables.

See also #

Press Esc to close