Editor

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

What it means

You wrote several names on the left of =, like a, b, c = ..., but the value on the right had fewer items than names. The message gives both counts: how many names you wrote (expected) and how many items there were (got).

1. Text with fewer parts than you expected

This raises the error

Broken
date = '18/10'
day, month, year = date.split('/')
print(day, month, year)
Output
Traceback (most recent call last):
  File "<exec>", line 2, in <module>
ValueError: not enough values to unpack (expected 3, got 2)

The fix

Fixed
date = '18/10'
parts = date.split('/')
if len(parts) == 3:
day, month, year = parts
else:
day, month = parts
year = '2026' # a default for the missing part
print(day, month, year)
Output
18 10 2026

Checking len(parts) before unpacking lets you handle short values instead of crashing. Print the parts first when you are not sure what the text holds.

Try fixing it yourself in the editor

2. One line in the data is missing a field

This raises the error

Broken
rows = ['Ana,90', 'Ben,85', 'Cara']
for row in rows:
name, score = row.split(',')
print(name, score)
Output
Ana 90
Ben 85
Traceback (most recent call last):
  File "<exec>", line 3, in <module>
ValueError: not enough values to unpack (expected 2, got 1)

The fix

Fixed
rows = ['Ana,90', 'Ben,85', 'Cara']
for row in rows:
parts = row.split(',')
if len(parts) != 2:
print('skipping bad row:', row)
continue
name, score = parts
print(name, score)
Output
Ana 90
Ben 85
skipping bad row: Cara

Real data often has a short or blank line. Checking the number of parts first lets you skip it or report it.

Try fixing it yourself in the editor

3. Unpacking more names than a function returns

This raises the error

Broken
def min_max(nums):
return min(nums), max(nums)
low, high, average = min_max([4, 8, 6])
print(low, high, average)
Output
Traceback (most recent call last):
  File "<exec>", line 4, in <module>
ValueError: not enough values to unpack (expected 3, got 2)

The fix

Fixed
def min_max_avg(nums):
return min(nums), max(nums), sum(nums) / len(nums)
low, high, average = min_max_avg([4, 8, 6])
print(low, high, average)
Output
4 8 6.0

A function returns exactly the values listed after return. Either unpack that many names or make the function return the extra value.

Try fixing it yourself in the editor

When you see this error, check

  • Compare expected (names on the left) with got (items on the right).
  • Print the value, or its len(), just before unpacking.
  • Check data for short or empty lines before splitting them into names.
  • str.partition() always returns three parts, so it is safe to unpack: key, _, value = line.partition('=').

All Python errors · How to read a traceback · Step through your code