ShareYourGroupShareYourGroupError Explainer

Python ValueError: too many values to unpack

A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.

The messageValueError: too many values to unpack (expected 2, got 3)

Make the error yourself

Run with Python 3.14.10. This is the smallest example we could find that shows it.

Python program (main.py)

name, city = "Asha,Pune,India".split(",")
print(name, city)

Output (the real message)

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    name, city = "Asha,Pune,India".split(",")
    ^^^^^^^^^^
ValueError: too many values to unpack (expected 2, got 3)

Why it happens

Writing name, city = something unpacks the parts of something into two names, so it needs exactly two parts. The text split on commas has three, so there is one too many. If there are too few, the message says "not enough values to unpack (expected 2, got 1)". It often happens with split, with the rows of a CSV file that have a different number of columns, and with for key, value in my_dict: (which needs .items()).

How to fix it

  1. Print the thing you unpack: print(parts) and count.
  2. Make the number of names match, or catch the rest with a starred name: name, city, *rest = parts.
  3. Limit the split: "a,b,c".split(",", 1) makes at most two parts.
  4. For dictionaries use for key, value in my_dict.items():.

The corrected version

Python program (main.py)

name, city, *rest = "Asha,Pune,India".split(",")
print(name, city, rest)

Output (the corrected version)

Asha Pune ['India']

How to avoid it

Check the length of data from files before unpacking it.

Questions

What does the star in *rest do?

It collects all the leftover values into a list (possibly empty).

This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.