Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does unpacking two values fail when my list has three items?

Quick answer

Without a starred target, Python requires the iterable to contain exactly as many items as there are assignment targets. So assigning a three-item list to two names fails. Add * to one target to collect the remaining items into a list: first, second, *rest = [10, 20, 30] gives rest the value [30]. If no items remain, rest is an empty list. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does first, second = [10, 20, 30] fail? I thought Python might just put the first two values in the variables and ignore the extra one.

Geez

In an assignment like this, Python expects the iterable on the right to have exactly as many items as there are targets on the left. Here there are two targets (first and second) but three items, so unpacking fails rather than silently discarding a value. The same mismatch happens if there are too few items. [S1]

If you want the first two values and intentionally want the remaining items grouped separately, make that explicit with a starred target:

first, second, *rest = [10, 20, 30]

Now first is 10, second is 20, and rest is [30]. The starred target collects the items not assigned to the other targets into a list. [S1]

Follow-up question

What if I want to keep the extra values instead of ignoring them? And does the starred target have to come last?

Geez

Keeping them is exactly what the starred target does: it receives the remaining items as a list. For the common case where the first two items have specific meanings and anything after them should be grouped together, put the star at the end:

name, score, *notes = ["Rina", 8, "late arrival", "needs review"]

For this input, name becomes "Rina", score becomes 8, and notes becomes ["late arrival", "needs review"].

The starred target can also appear in the middle, with targets after it. For example:

first, *middle, last = [10, 20, 30, 40]

Here first gets 10, last gets 40, and middle gets [20, 30]. An unpacking assignment can have one starred target, and the iterable must still have enough items for the targets outside the star. [S1]

Follow-up question

If there are only two values in the list, does *rest cause another error? I also want to know what to check when I see this error.

Geez

With two values, this is valid:

first, second, *rest = [10, 20]

first is 10, second is 20, and rest is []. The starred target can collect an empty list; it does not need to receive an extra item. [S1]

To investigate an unpacking error, count the targets and the items in the value at the moment the assignment runs. Check whether you intended an exact match or meant to collect extras. If a function sometimes returns different numbers of values, decide how each value should be handled instead of assuming Python will discard an extra one. A starred target handles extra items, but the iterable still needs enough items to fill the targets before and after the star. [S1]

Follow-up question

Can you give me a short reusable pattern for deciding which form to write?

Geez

Use exact unpacking when the number of items is part of what your code expects. Use a starred target when a variable number of remaining items is intentional and you want them grouped in a list. For example, if the first two entries have defined roles and later entries are optional:

label, value, *extras = ["item", 7]

Here extras is []; if more entries follow, they go into that list. The rule applies to iterable unpacking in assignment statements. It does not tell you what a particular function or data source is supposed to return, so check that separately if the values are unexpected. [S1]

A practical checklist

  • Count the assignment targets on the left side.

  • Check how many items the right-side iterable contains.

  • Use ordinary unpacking when the counts should match exactly.

  • Add one starred target when remaining items should be collected into a list.

  • Confirm there are enough items to fill every non-starred target.

A few more useful answers

Does the starred target receive a tuple?

No. In assignment unpacking, the items gathered by the starred target are collected into a list. [S1]

Can the starred target be empty?

Yes. If the other targets are filled and no items remain, the starred target receives an empty list. [S1]

Does adding `*rest` make any input length valid?

No. The iterable must still have enough items to fill the targets outside the star; the star collects the remaining items. [S1]

Sources

References used for this guide.

  1. [S1] Python Language Reference: Assignment statements and unpacking
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work