Concepts / Lists: Mutable Sequences

Lists: Mutable Sequences

Unpacking is a syntactic feature that assigns multiple variables from a sequence in one statement, equivalent to index-based assignment.

  • Programming

One Sequence, Several Names

Suppose a sequence contains three values and you want each value to have its own variable. Python lets you assign all three variables in one statement. This operation is called unpacking: the sequence is opened into individual elements, and those elements are assigned to the variables on the left.

python
Output (expected)
x receives 12 and y receives 7
containscontainsassigned toassigned to(12, 7)tupleindex 012x12index 17y7
How do the elements of a tuple map to individual variables in one assignment?

Reading the Assignment

In an unpacking assignment, the variables on the left correspond to sequence elements by position. The first variable receives the first element, the second variable receives the second element, and so on. The important requirement is that the number of variables must exactly match the number of elements.

parts = ["north", "east", "up"] first, second, third = parts

Unpacking by Index

Connecting unpacking to indexes

Relate the unpacking assignment first, second = values to separate index-based assignments.

Start with the sequence: Assume values contains two elements in a known order.

Use unpacking: The statement first, second = values assigns the first sequence element to first and the second sequence element to second.

Rewrite by index: The same result can be expressed as first = values[0] followed by second = values[1].

Unpacking is syntactic sugar for the corresponding index-based assignments.

index 0index 1assignassignposition 0position 1valuesa sequencevalues[0]first elementfirstassigned separatelyvaluesa sequencefirstunpacked targetvalues[1]second elementsecondassigned separatelysecondunpacked target
How does one unpacking statement correspond to separate assignments using sequence indexes?

Unpacking does not introduce a different kind of sequence element. It provides shorter syntax for assigning elements according to their indexes.

Tuples, Lists, and Strings

Unpacking works uniformly with tuples, lists, strings, and other sequence types. The sequence type can differ, but the assignment rule remains the same: elements are assigned to target variables in order.

python
Output (expected)
a receives 3 and b receives 4; left receives L and right receives R; first receives c, second receives a, and third receives t

Counting Before Assigning

The most common unpacking error is a mismatch between the number of target variables and the number of sequence elements. Python requires an exact match. If the counts differ, the unpacking operation causes a ValueError.

withwithwithwith2 targetsleft, right3 elementsa, b, cValueErrorcounts differ3 targetsfirst, second, third2 elementsa, bValueErrorcounts differ
What happens when the sequence contains more or fewer elements than the target variables?
  • Providing fewer target variables than sequence elements

    The list has three elements, but the assignment has only two targets.

    Fix: Use three target variables, such as first, second, third = [10, 20, 30], or change the sequence so it has two elements.

  • Providing more target variables than sequence elements

    The assignment has three targets, but the list has only two elements.

    Fix: Make the number of targets equal to the number of elements.

  • Assuming the sequence type changes the counting rule

    The string contains three sequence elements, so two targets cannot receive all of them.

    Fix: Provide three targets for the three characters, or use a sequence with two elements.

Repairing a Mismatch

What do you think happens?

What happens when Python evaluates left, right = ["west", "east", "north"]?

  • left receives west and right receives east; north is ignored
  • left receives the entire list and right receives nothing
  • The assignment causes a ValueError
  • Python automatically creates a third variable
Reveal answer

Answer: The assignment causes a ValueError.

The list contains three elements but the left side contains only two target variables. Unpacking requires an exact count match.

When an unpacking statement fails, count both sides before changing anything else. Count the sequence elements and count the target variables. Then make the counts equal, or replace unpacking with an approach that matches the result you actually need.

Practice the Mapping

MEDIUM

For each assignment, decide whether unpacking succeeds. If it succeeds, state which value each variable receives. If it fails, identify the count mismatch and revise the target list.

Hints
  • Count the elements in each sequence.
  • Count the variables on the left.
  • Match values and variables by position.
python
  1. Check each statement by applying the same three-step process: identify the sequence, count its elements, and map those elements to the targets in order.

Key Takeaways

  • Unpacking assigns multiple variables from one sequence in a single statement.
  • The first target receives the first element, the second target receives the second element, and so on.
  • Tuples, lists, strings, and other sequence types can be unpacked using the same positional rule.
  • The number of targets must exactly match the number of sequence elements; otherwise Python causes a ValueError.
  • Unpacking is syntactic sugar for equivalent index-based assignments.