ValueError: not enough values to unpack (Fix)

This error happens when Python tries to unpack values into multiple variables, but there are fewer values than expected.

A common message looks like this:

ValueError: not enough values to unpack (expected 2, got 1)

In this guide, you will learn what this means, why it happens, and how to fix it in common cases like lists, function returns, loops, and split().

Quick fix

Make sure the number of variables matches the number of values.

values = [1, 2]
a, b = values   # works

# if the number of items may vary:
first, *rest = values

The *rest part collects any remaining items into a list. This is useful when the number of values may change.

What this error means

Unpacking means assigning items from a list, tuple, or other iterable to separate variables.

For example:

a, b = [10, 20]

Here, Python unpacks:

  • 10 into a
  • 20 into b

The error happens when:

  • Python expects multiple values
  • The right side has fewer items than the left side expects
  • For example, it expected 2 values but only got 1

Simple example that causes the error

Here is a small example:

a, b = [1]

This raises:

ValueError: not enough values to unpack (expected 2, got 1)

Why?

  • a and b need 2 values
  • [1] contains only 1 item
  • Python cannot fill both variables

A working version would be:

a, b = [1, 2]
print(a)
print(b)

Output:

1
2

Why it happens

This error usually appears in one of these situations:

  • A list or tuple has fewer items than expected
  • A function returns fewer values than your code expects
  • Loop code unpacks items with the wrong structure
  • split() returns fewer parts than expected

Example: unpacking a short tuple

point = (5,)
x, y = point

point contains only one value, but x, y expects two.

Example: function returns too few values

def get_user():
    return "Alice"

name, age = get_user()

This fails because the function returns only one value, but the code expects two.

Example: loop items do not match

items = [(1, 2), (3,)]

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

The second item (3,) has only one value, so unpacking fails.

How to fix it

Use the fix that matches your situation.

1. Match the number of variables to the number of values

If you have one value, unpack into one variable.

value = [1]
a = value[0]
print(a)

Or if you expect two values, make sure the data really has two items:

a, b = [1, 2]
print(a, b)

2. Check the actual data before unpacking

Sometimes the data is not what you think it is. Print it first.

values = [1]

print(values)
print(type(values))
print(len(values))

Output:

[1]
<class 'list'>
1

This quickly shows why unpacking into two variables would fail.

3. Use starred unpacking when length may vary

If the number of items can change, use starred unpacking.

values = [10, 20, 30, 40]

first, *rest = values

print(first)
print(rest)

Output:

10
[20, 30, 40]

You can also do this:

values = [10]

first, *rest = values

print(first)
print(rest)

Output:

10
[]

This is a safe pattern when you only need the first item and want the rest collected automatically.

4. Fix function return values or unpack fewer variables

If a function returns one value, do not try to unpack two.

Wrong:

def get_total():
    return 100

subtotal, tax = get_total()

Right:

def get_total():
    return 100

total = get_total()
print(total)

Or change the function so it returns the expected number of values:

def get_total():
    return 100, 20

subtotal, tax = get_total()
print(subtotal, tax)

5. Validate results before assigning

This is especially important with user input, file data, or split text.

text = "Alice"

parts = text.split(",")

if len(parts) == 2:
    name, age = parts
    print(name, age)
else:
    print("Input is missing a comma")

If you want to learn more about handling unexpected cases, see how to handle exceptions in Python.

Fix when using split()

split() is a very common cause of this error.

Example:

text = "Alice"
name, age = text.split(",")

This fails because "Alice" does not contain a comma, so split(",") returns:

['Alice']

That is only one part, not two.

text = "Alice"
print(text.split(","))

Output:

['Alice']

Use a condition before unpacking

text = "Alice,25"
parts = text.split(",")

if len(parts) == 2:
    name, age = parts
    print("Name:", name)
    print("Age:", age)
else:
    print("Text is not in the expected format")

Another safe approach

text = "Alice"
parts = text.split(",")

name = parts[0]
age = parts[1] if len(parts) > 1 else None

print(name)
print(age)

If you need a full explanation of this method, see Python string.split() method.

Fix when looping

This error often appears in loops that unpack each item.

Problem example:

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

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

The first two items work. The last item does not because (5,) has only one value.

How to find the bad item

Print each item before unpacking:

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

for item in items:
    print(item)

Output:

(1, 2)
(3, 4)
(5,)

Now the inconsistent data is easier to spot.

Safe version

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

for item in items:
    if len(item) == 2:
        a, b = item
        print(a, b)
    else:
        print("Bad item:", item)

This is especially useful when reading rows from files or CSV-like data.

Debugging steps

If you are not sure why the error is happening, use these steps:

  1. Print the value you are unpacking
  2. Check its type with type()
  3. Check its length with len() if possible
  4. Read the full error message carefully

Useful commands:

print(value)
print(type(value))
print(len(value))
print(text.split(','))
for item in items:
    print(item)

The full error message often tells you exactly what went wrong, such as:

  • expected 2, got 1
  • expected 3, got 2

That gives you a direct clue about the mismatch.

Common mistakes

These are common causes of this error:

  • Unpacking a short list or tuple
  • Using split() on text that does not contain the expected separator
  • Expecting a function to return two or more values when it returns one
  • Looping through nested data with inconsistent item lengths
  • Reading rows from a file or CSV-like source with missing fields

It can also help to compare this error with ValueError: too many values to unpack, which is the opposite problem.

FAQ

What does unpacking mean in Python?

Unpacking means assigning items from a list, tuple, or other iterable to separate variables.

How is this different from "too many values to unpack"?

This error means there are fewer values than expected. The other error means there are more values than expected.

How do I handle variable-length data safely?

Use starred unpacking, such as:

first, *rest = values

This lets Python put the first item into first and collect the remaining items into rest.

Can split() cause this error?

Yes. If split() returns fewer parts than your variables expect, unpacking will fail.

See also