What this thread does
ValueError: too many values to unpack (expected 2)
Python cannot assign an iterable's items to the requested number of targets.
First action — for a repeatable list or tuple, insert print(type(values).__name__, len(values)) before unpacking, using the actual right-hand value.
Official sources establish the item-count rule and starred assignment; they provide no universal repair for missing items, and the reader's producer remains untested.
Not for: Python dictionary changed size during iteration; see python-dictionary-changed-size-locals-tracing-snapshot.
How to diagnose and fix
The companion message is ValueError: not enough values to unpack (expected 2, got 1); use the same count inspection first.
1. Capture the right-hand value once at the failing assignment. For a list or tuple, compare its length with the number of targets. Do not consume a one-shot iterator merely to print its contents and then try to unpack it again. 2. Without a starred target, require exactly one item per target. This is the official assignment rule, not evidence that a particular file or API is malformed. 3. If extra items are part of the intended contract, use a starred target. For example, first, *rest = values retains the extras in a list. Do not introduce this change if each record must contain exactly two fields. 4. For insufficient items, inspect the producer and the failing input. The official reference does not prescribe padding, skipping or repairing that input. Choose a policy from the application's contract before editing it.
values = [10, 20, 30]
# left, right = values
# ValueError: too many values to unpack (expected 2)
first, *rest = values
assert first == 10
assert rest == [20, 30]
values = [10]
# left, right = values
# ValueError: not enough values to unpack (expected 2, got 1)
Diagnostic: ValueError: not enough values to unpack (expected 2, got 1)
CPython's evaluator source contains too many values to unpack (expected %d) and the two-part template not enough values to unpack (expected %d, got %d). The concrete examples above substitute two targets and one actual item. CPython 3.12 source checked 2026-10-28.
With starred targets, the minimum required count still matters: first, *middle, last = [10] corresponds to ValueError: not enough values to unpack (expected at least 2, got 1). A star collects extras; it does not manufacture required values. This follows the reference's starred-target rule.
What to test
Exercise zero, one, two and three items against the actual assignment. If the input is a parsed row, include one real failing row in a regression test. Check whether extra fields must be retained or rejected, and whether empty starred results are valid.
Pitfalls
- Adding
*restto hide an unexpected schema change. - Assuming the numbers in an example are fixed; they depend on the assignment and actual iterable.
- Calling
list()on a generator for inspection and then unpacking the exhausted original.
Public evidence
kopihitampekat's recon-ng report #174, opened 2020-08-27 (checked 2026-10-28), quotes “not enough values to unpack (expected 2, got 1)” at host, address = line.split(","). Owner lanmaster53 marked it a duplicate of #172. This establishes a real failing split/unpack site; it does not establish a generally applicable parsing fix.
A prompt to give your agent
Read the linked official assignment reference and evaluator source instead of guessing. Locate the exact failing right-hand value and count the targets. Preserve one-shot iterators. Explain whether extras are intentional before adding a starred target. For insufficient items, show the failing input and the producer's contract; do not invent padding or drop data silently.
Limits
The diagnostic is not a fix for every parser or library return type. No universal insufficient-items fix is stated in the cited reference. Public evidence is one historical application report, not a confirmed general remedy. The example code and reader-specific producer have not been run.
Started from a post by @kopihitampekat, packaged by the editor. Original post