← Back

Home › Errors › ValueError

ValueError: not enough values to unpack

Exception: ValueErrorCategory: runtimeRaised at: the assignment line that unpacks an iterable into names

What this error means

You assigned an iterable to more names than it has items. The message even does the arithmetic for you: expected 3, got 1 means three names on the left, one item on the right.

Unpacking is a length check, not a conversion. year, month, day = "2026-10-11" fails with too many values to unpack (11 characters, 3 names); year, month, day = parts fails with this error whenever parts came out shorter than three.

Cause 1 — split() with a separator that is not in the string

The single most common source. When the separator is absent, split() does not fail — it returns a one-item list containing the whole string, and the unpacking blows up:

row = "2026/10/11"
year, month, day = row.split("-")   # split returns ['2026/10/11']
Traceback (most recent call last):
  File "demo.py", line 2, in <module>
    year, month, day = row.split("-")
    ^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 3, got 1)

Print the intermediate value and the cause is obvious: print(row.split("-")) → ['2026/10/11']. The real data used /, or the column is empty, or the file has Windows line endings and the last field is '11\r'. Always inspect the list before unpacking it.

Cause 2 — a row that is genuinely short

CSV rows, user input, API payloads and database rows do not all have the same width. One truncated line in a 10,000-line file is enough:

with open("scores.csv", encoding="utf-8") as f:
    for lineno, line in enumerate(f, start=1):
        parts = line.strip().split(",")
        if len(parts) != 3:
            print(f"line {lineno} has {len(parts)} fields: {parts!r}")
            continue
        name, subject, score = parts

Cause 3 — a function that returned fewer values

A function with two return paths — one returning a tuple, one returning None or a single value — unpacks fine on the happy path and fails on the other:

def split_name(full):
    if " " in full:
        first, last = full.split(" ", 1)
        return first, last
    return full            # one value, not two

first, last = split_name("Asha")   # ValueError: not enough values to unpack

Four ways to fix it

parts = row.split("-")
year, month, day = (parts + [None, None])[:3]   # tolerant, always three names

How to debug it

  1. Print the right-hand side first: parts = row.split('-'); print(len(parts), parts). The count and the repr tell you everything.
  2. Read the numbers in the message — expected 3, got 1 means three names on the left, one item on the right.
  3. Check for a stray \r from Windows files: open with newline="" in the csv module, or .strip() each line.
  4. If it comes from a function call, read every return in that function — they must all return the same shape.
  5. For CSV, use csv.reader instead of str.split(','): it handles quoted fields that contain commas, which silently change the field count.

Fix it interactively

Unpacking practice, free in the browser:

Practice more Python bugs →

Related messages worth knowing

🇮🇳 Hindi में समझें

ValueError: not enough values to unpack (expected 3, got 1) ka matlab hai aapne zyada naam likhe, aur right side par kam items hain. Sabse common wajah: split() ka separator string me maujood hi nahi hai — tab split fail nahi hota, balki poori string ka ek-item list de deta hai, jaise "2026/10/11".split("-") → ['2026/10/11']. Debug karne ka tareeka: pehle parts = row.split('-') likho, phir print(len(parts), parts). Fix ke chaar raste: len(parts) check karke skip karo; star unpacking lo (first, *rest = parts); pad kar do (a, b, c = (parts + [None, None])[:3]); ya sirf ek split chahiye to partition() use karo jo kabhi error nahi deta.

Hindi practice questions देखें →