Skip to content

ValueError: not enough values to unpack

Error message

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

Quick summary

You tried to unpack a sequence into a number of variables that doesn't match the sequence's length.

Why this happens

  • Unpacking a tuple/list with fewer elements than variables

  • Unpacking a string split result with the wrong number of parts

  • Expecting more return values than a function provides

Minimal example

✗ Broken code
data = (1, 2)
a, b, c = data  # ValueError: not enough values to unpack
✓ Fixed code
data = (1, 2)
a, b = data  # Correct: 2 values, 2 variables
# Or use * for flexible unpacking:
# a, *rest = data

The tuple has 2 elements but 3 variables were used. Match the variable count to the sequence length.

How to diagnose

  • Check the length of the sequence being unpacked

  • Verify the number of variables matches the sequence length

  • Print the sequence before unpacking to see its contents

How to fix

  • Match the number of variables to the sequence length

  • Use * to collect extra values: a, *rest = sequence

  • Check the length before unpacking

How to prevent

  • Verify sequence length before unpacking

  • Use * unpacking for variable-length sequences

  • Test with representative data

Related Resources

Related Glossary

Related Lessons

← Back to language