HOWTO · Python

How to Fix ValueError: Not Enough Values to Unpack in Python

The ValueError: not enough values to unpack in Python occurs in a few different scenarios like iterating over dictionary keys but accessing the values too, or taking two inputs in the input statement but providing less than two values, or assigning fewer values to the expected number of values causes the ValueError not enough values to unpack.

On this page

Python raises ValueError: not enough values to unpack whenever an assignment or a loop header asks for more variables than the iterable on the right can supply. You will meet it in direct assignments, in input that is split into too few fields, and in dictionary loops that unpack each pair into the wrong number of names. This guide shows how to read the message and how to fix each situation.

Why Python raises ValueError: not enough values to unpack

Unpacking assigns the items of an iterable — a tuple, list, string, or view object — to a matching list of names. In a, b, c = 3, 5, Python counts both sides before assigning anything: three names on the left, two values on the right. Because fewer values arrived than targets were written, the interpreter raises a ValueError whose message reports both counts, expected 3, got 2. Those two numbers are the diagnosis — the first is how many targets you wrote, the second is how many values the iterable actually produced.

Run this example to see the message Python reports:

try:
    a, b, c = 3, 5
except ValueError as error:
    print("ValueError:", error)
ValueError: not enough values to unpack (expected 3, got 2)

Without the try/except, the program stops at the assignment line. The same mismatch appears for any iterable that is shorter than the target list — a, b, d, e = [1, 2, 3] asks for four names from a three-item list, and a, b, c = 2 asks for three names from a single integer, which Python cannot unpack at all.

Fix unpacking errors in assignments and input

The simplest fix is to make both sides agree: the number of names on the left must match the number of values the right side can supply. When the values come from input(), remember that input() always returns a string, so split() decides how many fields the assignment actually receives. The program below asks for three comma-separated numbers and gets three, so the unpack works and the multiplication runs. Run this example:

import sys
from io import StringIO

# Simulate typing "2,2,2" at the prompt.
sys.stdin = StringIO("2,2,2\n")

x, y, z = input("Enter three numbers to multiply: ").split(",")
product = int(x) * int(y) * int(z)
print("The product of x, y, and z is:", product)
Enter three numbers to multiply: The product of x, y, and z is: 8

The sys.stdin line stands in for a user typing 2,2,2; in a real program you call input() normally and the person at the keyboard decides how many fields arrive. When someone types fewer fields than the targets need, the split list is too short and the same ValueError appears:

import sys
from io import StringIO

# Simulate typing "1,2" at the prompt.
sys.stdin = StringIO("1,2\n")

try:
    x, y, z = input("Enter values for x, y and z: ").split(",")
except ValueError as error:
    print("ValueError:", error)
Enter values for x, y and z: ValueError: not enough values to unpack (expected 3, got 2)

If your right side can legitimately vary in length, a starred target absorbs the surplus: first, *rest = [1, 2, 3] binds first to 1 and rest to [2, 3]. A star only helps when extra items are the problem — when too few values arrive, match the counts or validate the input first.

Fix dictionary unpacking with .items()

A dictionary stores key-value pairs. Iterating the dictionary itself yields only its keys, while .items() returns a view of (key, value) pairs in which every pair holds exactly two values. You can see the three standard views:

student = {"name": "Zeeshan Afridi", "degree": "BSSE", "section": "A"}

print("Keys:", student.keys())
print("Values:", student.values())
print("Items:", student.items())
Keys: dict_keys(['name', 'degree', 'section'])
Values: dict_values(['Zeeshan Afridi', 'BSSE', 'A'])
Items: dict_items([('name', 'Zeeshan Afridi'), ('degree', 'BSSE'), ('section', 'A')])

Because every element from .items() is a two-value pair, a loop header with three targets fails on the first iteration — it is the pair that supplies the values, not the method, that fixes the count:

student = {"name": "Zeeshan Afridi", "degree": "BSSE", "section": "A"}

try:
    for k, v, l in student.items():
        print(k, v, l)
except ValueError as error:
    print("ValueError:", error)
ValueError: not enough values to unpack (expected 3, got 2)

The fix is to unpack each pair into two names, one for the key and one for the value:

student = {"name": "Zeeshan Afridi", "degree": "BSSE", "section": "A"}

for key, value in student.items():
    print("Key:", key)
    print("Value:", value)
Key: name
Value: Zeeshan Afridi
Key: degree
Value: BSSE
Key: section
Value: A

When you only need the keys, iterate the dictionary directly with for key in student:; use .values() when you only need the values. Be careful not to unpack a bare key loop — for k, v in student: tries to unpack each key string itself, so multi-character keys fail with too many values to unpack.

Validate input and handle unpacking errors

Two habits keep this error away from users. First, count the fields before unpacking: split() hands you a list, and len() tells you whether it matches the number of names you plan to assign, so you can ask for the data again with a clear prompt:

import sys
from io import StringIO

# Simulate typing "1,2" at the prompt.
sys.stdin = StringIO("1,2\n")

parts = input("Enter three numbers to multiply: ").split(",")

if len(parts) != 3:
    print("Please enter exactly three numbers separated by commas.")
else:
    x, y, z = (int(part) for part in parts)
    print("The product of x, y, and z is:", x * y * z)
Enter three numbers to multiply: Please enter exactly three numbers separated by commas.

Second, wrap the unpack-and-convert block in try/except — the Python keywords are try and except, not the try-catch spelling used in some other languages. Because int() also raises ValueError for non-numeric fields, one except clause covers both a short field list and values that cannot be converted:

import sys
from io import StringIO

# Simulate typing "2,two,4" at the prompt.
sys.stdin = StringIO("2,two,4\n")

try:
    x, y, z = input("Enter three numbers to multiply: ").split(",")
    product = int(x) * int(y) * int(z)
    print("The product of x, y, and z is:", product)
except ValueError as error:
    print("Could not use that input:", error)
Enter three numbers to multiply: Could not use that input: invalid literal for int() with base 10: 'two'

Catching the exception lets the program recover, but it does not fix the data. Prefer the length check — plus any numeric or format checks your input needs — so you can tell the user exactly what to type, and keep try/except as the safety net around the unpack itself.