ValueError: Not Enough Values to Unpack

ValueError: not enough values to unpack (expected 3, got 2)

Quick Answer

You are unpacking into more variables than the iterable has elements. Check the length before unpacking or provide defaults.

Why This Happens

This is the inverse of 'too many values.' When you write a, b, c = iterable and it only has 2 elements, Python cannot fill all variables.

The Problem

line = 'Alice,30'
name, age, email = line.split(',')

The Fix

parts = line.split(',')
name = parts[0]
age = parts[1] if len(parts) > 1 else None
email = parts[2] if len(parts) > 2 else None

Step-by-Step Fix

  1. 1

    Check element count

    Verify length matches variables before unpacking.

  2. 2

    Use indexing with defaults

    Access by index with length checks.

  3. 3

    Pad the iterable

    Extend: parts += [None] * (3 - len(parts)).

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free