ValueError: Too Many Values to Unpack

ValueError: too many values to unpack (expected 2)

Quick Answer

You are unpacking an iterable into fewer variables than it has elements. Add more variables or use * to capture the rest.

Why This Happens

When you write a, b = iterable, Python expects exactly 2 elements. If there are more, you get this error. Python 3 allows * syntax to capture extras.

The Problem

data = 'Alice,30,alice@example.com'
name, age = data.split(',')

The Fix

data = 'Alice,30,alice@example.com'
name, age, email = data.split(',')
# Or: name, *rest = data.split(',')

Step-by-Step Fix

  1. 1

    Count the elements

    Print the iterable to see how many elements it has.

  2. 2

    Use * to capture extras

    Use first, *rest = iterable.

  3. 3

    Limit the split

    Use str.split(',', maxsplit=1) to control part count.

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