python value_error ai_generated true

ValueError: too many values to unpack (expected 2)

ID: python/valueerror-too-many-values-unpack

Also available as: JSON · Markdown
92%Fix Rate
93%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

Unpacking assignment count mismatch. Common with CSV, split(), and tuple returns.

generic

Workarounds

  1. 92% success Check actual length of data; use *rest for variable-length unpacking
    first, *rest = line.split(',')

    Sources: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

  2. 88% success Validate data format before unpacking
    parts = line.split(',')
    if len(parts) != 3:
        raise ValueError(f'Expected 3 columns, got {len(parts)}: {line!r}')
    name, age, email = parts

    Sources: https://docs.python.org/3/library/csv.html

Dead Ends

Common approaches that don't work:

  1. Pad or truncate the iterable 70% fail

    Silently drops or fabricates data

  2. Catch ValueError and skip the row 60% fail

    Loses data without understanding why

Error Chain

Frequently confused with: