ProjectStack
python

ValueError: too many values to unpack

You tried to unpack a sequence into variables, but the sequence has more items than you have variables to receive them. Python requires an exact match (or a starred variable) when unpacking.

Common causes

  • Unpacking a list/tuple with more items than target variables: a, b = [1, 2, 3]
  • A function returned more values than expected
  • Iterating over rows of data and the row has extra columns
  • A CSV or split() result has more elements than you're unpacking into

How to fix it

  1. Add a starred variable to absorb the extra values: a, b, *rest = [1, 2, 3, 4]
  2. Or just access by index instead of unpacking: items = get_values(); a = items[0]
  3. Check what the function actually returns: print(get_values())
  4. Use only the values you need: a, b = values[:2]

Example

Traceback (most recent call last): File "app.py", line 3, in <module> x, y = get_coordinates() ValueError: too many values to unpack (expected 2)

Unpacking into two variables when get_coordinates() returns a tuple of three values

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.