Concepts / Extended Unpacking with the Asterisk Operator

Extended Unpacking with the Asterisk Operator

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

Unpacking lets one statement assign several variables from the elements of a sequence. Instead of retrieving each element separately by index, you place several receiving names on the left side of the assignment. Python then matches those names with the sequence elements.

What do you think happens?

A sequence contains the values 10, 20, and 30. If three names appear on the left side of an unpacking assignment, which value does the second name receive?

  • 10
  • 20
  • 30
Reveal answer

Answer: 20

Unpacking follows the sequence order: the first name receives the first element, the second name receives the second element, and the third name receives the third element.

How the Starred Name Collects Elements

Extended unpacking adds an asterisk before one receiving name. The names before and after the starred name receive their corresponding elements, while the starred name collects the remaining elements between them. This is useful when the sequence has more elements than the fixed names can receive individually.

assignedcollectedcollectedassigned10first elementfirst1020remaining elementmiddle[20, 30]30remaining elementlast4040last element
How does the starred variable collect the middle elements while the other variables receive specific positions?
python
Output
first is 10, middle is [20, 30], and last is 40.

Unpacking as Indexed Assignment

Unpacking is syntactic sugar for index-based assignment. In other words, the compact unpacking statement represents the same element-by-element relationship that you could express by selecting sequence elements by their indexes. The unpacking form emphasizes the order of the values without writing each index separately.

unpack in ordermatchesmatchesmatchesfirst, second,thirdone unpacking statementfirstitems[0]items[10, 20, 30]seconditems[1]thirditems[2]
How does one unpacking statement correspond to assigning sequence elements by their indexes?

items = [10, 20, 30] first, second, third = items

Matching Names to Elements

Without a starred receiving name, the number of variables must exactly match the number of elements in the sequence. Too few or too many names creates a mismatch, and the mismatch causes a ValueError. A starred name changes this rule by receiving the remaining elements, so the fixed names do not each need a one-to-one match with every element.

exact countcount mismatchstarred name collects remaining elements3 names, 3 elementsvalid2 names, 3 elementsValueError2 fixed names, 3elementsone starred namefixed values plusremaining valuesvalid
What happens when the number of variables does not match the number of sequence elements, and how does a starred variable change that rule?
  • Using fewer names than sequence elements without a starred name

    There are three elements but only two receiving names, so the counts do not match.

    Fix: Use three receiving names, or use a starred name when one variable should collect the remaining elements.

  • Using more names than sequence elements

    The sequence does not contain enough elements to supply every receiving name.

    Fix: Make the number of names match the number of elements, or redesign the assignment with a starred name where appropriate.

  • Forgetting that order controls the assignment

    The first name receives the first element and the last name receives the last element; the starred name receives the elements between them.

    Fix: Read the receiving names from left to right and identify which name is intended to collect the remaining elements.

Tuples, Lists, and Strings

The same unpacking idea works with tuples, lists, strings, and any sequence type. What changes is the sequence being unpacked; the assignment pattern remains the same. With a string, the sequence elements are its characters, so each receiving name is assigned a character according to its position.

SequenceExample patternWhat is assigned
Tupleleft, right = (10, 20)The tuple elements are assigned in order
Listleft, right = [10, 20]The list elements are assigned in order
Stringfirst, second = "AB"The string's characters are assigned in order

Unpacking uses the order of elements in each sequence type.

python
Output
x is 3, y is 4, first is "C", middle contains ["A"], and last is "T".

Practice the Pattern

EASY

Suppose values is the list [5, 10, 15, 20, 25]. Write one unpacking statement that assigns 5 to first, 25 to last, and all values between them to middle. Then describe the equivalent index-based assignments for first and last.

Hints
  • Place first before the starred name and last after it.
  • The starred name should collect the elements between the first and last positions.
  • The first and last fixed positions correspond to the beginning and end of the sequence.

Separating the Ends from the Middle

Unpack [5, 10, 15, 20, 25] so that the first value goes to first, the last value goes to last, and the values between them go to middle.

Place the fixed names: Put first at the beginning and last at the end of the receiving pattern.

Mark the collecting name: Put an asterisk before middle so it collects the remaining elements.

Match the sequence positions: first receives 5, last receives 25, and middle receives the values between them.

first is 5, middle contains [10, 15, 20], and last is 25.

Reliable Unpacking Habits

  • Count the sequence elements and receiving names before using ordinary unpacking.
  • Use a starred receiving name when one variable should collect the remaining elements.
  • Read the assignment from left to right so each fixed name is connected with the intended sequence position.
  • Remember that the same unpacking pattern can be applied to tuples, lists, strings, and other sequence types.
  • When debugging a ValueError, first check whether the number of variables exactly matches the number of elements or whether a starred name is needed.
  1. Unpacking assigns multiple variables from a sequence in one statement. Ordinary unpacking requires an exact match between receiving names and sequence elements. Extended unpacking uses an asterisk before one receiving name so that name collects the remaining elements. The pattern works across tuples, lists, strings, and other sequence types, and it represents the same positional relationship as index-based assignment.

Key Takeaways

  • Unpacking assigns several variables from a sequence in one statement.
  • The assignment follows sequence order and corresponds to index-based assignment.
  • Without a starred name, the number of variables must exactly match the number of elements.
  • A starred receiving name collects the remaining elements.
  • Tuples, lists, strings, and other sequence types can be unpacked using the same pattern.