
This error happens when you use tuple unpacking (assigning multiple variables at once) but the number of variables on the left doesn’t match the number of items on the right.
⚡ Quick Fix: ValueError: not enough values to unpack (expected X, got Y) — Python Tuple Unpacking Fix for Lists, Dictionary .items(), and CSV Rows
You put more variables on the left side of the assignment than items exist on the right — Python ran out of values before filling every variable.
# WRONG — list has 1 item, unpacking demands 2 variables
my_list = [1]
a, b = my_list # ValueError: not enough values to unpack (expected 2, got 1)
# WRONG — list has 3 items, unpacking demands 2 variables
my_list = [1, 2, 3]
a, b = my_list # ValueError: too many values to unpack (expected 2)
# WRONG — looping a dict without .items() gives keys, not key-value pairs
my_dict = {"name": "Alice", "age": 25}
for key in my_dict:
a, b = key # ValueError — 'name' has 4 characters, not 2 items
# RIGHT — match variable count exactly to item count
a, b = [1, 2] # works
# RIGHT — unpack dict key-value pairs with .items()
for key, value in my_dict.items():
print(f"{key} = {value}")
# RIGHT — use * to capture variable-length remainders
first, *rest = [1, 2, 3, 4] # first = 1, rest = [2, 3, 4]The three causes below cover list unpacking, dictionary loops, and CSV row mismatches — with the exact fix for each.
In Python, this is known as a ValueError not enough values to unpack issue.
The Cause (Not Enough Values)
my_list = [1] a, b = my_list # CRASH! # Error: ValueError: not enough values to unpack (expected 2, got 1)
Python tries to put my_list[0] into a, but there’s nothing left over to put into b.
The Cause (Too Many Values)
You can also get the opposite error:
my_list = [1, 2, 3] a, b = my_list # CRASH! # Error: ValueError: too many values to unpack (expected 2)
Common Scenario: Dictionary Loops
This is the #1 place beginners see this. They forget that .items() returns two things (key and value).
Problem Code:
my_dict = {"name": "Alice", "age": 25}
for key in my_dict.items(): # .items() returns [('name', 'Alice'), ('age', 25)]
print(key)
# Output: ('name', 'Alice')
# ('age', 25)Problem Code 2:
for key in my_dict: # Just looping a dict gives keys
a, b = key # CRASH! ValueError (trying to unpack 'name')The Fix: Always unpack .items() into two variables.
for key, value in my_dict.items():
print(f"{key} = {value}")ValueError: not enough values to unpack (expected X, got Y) — The Unpacking Rule and Three Fixes
ValueError: not enough values to unpack (expected X, got Y) enforces one rule: the variable count on the left must match the item count on the right, exactly.
Read the error numbers first. “expected 2, got 1” tells you Python found one item where you demanded two. Print the variable you’re unpacking — print(my_list), print(type(my_list)) — and count the actual items before adjusting the variable count.
Three patterns cover every form of this error.
Fixed-length unpacking: match variables to items one-for-one. a, b = [1, 2] works. a, b = [1] crashes. If the source is a function return value, check whether it always returns the same number of items — a function that returns a tuple on success and None on failure will crash any fixed unpacking.
Dictionary loops: always use .items() and always unpack into two variables. for key, value in my_dict.items(): is the only correct pattern. for key in my_dict: gives you keys only — unpacking a string key into two variables fires ValueError immediately.
Variable-length unpacking: use * to capture the remainder when the item count varies. first, *rest = my_list assigns the first item to first and collects everything else into rest as a list. a, b, *_ = my_list discards the remainder with _. This pattern handles CSV rows, API responses, and any data source where the length isn’t guaranteed.
first, *rest = [1, 2, 3, 4] # first = 1, rest = [2, 3, 4]
a, b, *_ = [1, 2, 3, 4, 5] # a = 1, b = 2, remainder discarded





