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
- Guard the length — check
len(parts)and skip, log or raise a message that names the offending line. Best for files and user input. - Take what you need with star unpacking —
first, *rest = partsalways succeeds when there is at least one item;first, *_, last = partsneeds at least two. Use*_to discard a variable tail. - Supply defaults while unpacking —
a, b, c = (parts + [None, None, None])[:3]pads short rows instead of crashing. Explicit and easy to read. - Use partition when you only need one split —
head, sep, tail = text.partition(":")never raises and tells you insepwhether the separator was found at all.
parts = row.split("-")
year, month, day = (parts + [None, None])[:3] # tolerant, always three names
How to debug it
- Print the right-hand side first:
parts = row.split('-'); print(len(parts), parts). The count and the repr tell you everything. - Read the numbers in the message —
expected 3, got 1means three names on the left, one item on the right. - Check for a stray
\rfrom Windows files: open withnewline=""in thecsvmodule, or.strip()each line. - If it comes from a function call, read every
returnin that function — they must all return the same shape. - For CSV, use
csv.readerinstead ofstr.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
- too many values to unpack (expected 2) — the mirror error — the iterable is longer than the names.
- ValueError: the parent guide — right type of object, wrong value inside it.
- TypeError: string indices must be integers — the next error in line when you unpack a string by accident.
🇮🇳 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.