Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Starred Tuple Unpacking: Syntax and Use Cases

Learn how Python's starred tuple unpacking uses the * operator to assign values, handle variable-length sequences, and avoid common errors.

~3 min readPython
#Python#tuple unpacking#starred expressions#iterable unpacking
Illustration showing a tuple being split into fixed first and last elements with a starred middle section in Python

Starred tuple unpacking uses the * operator during assignment to capture a variable number of elements from an iterable. The syntax is easy to recognize:

python
first, *middle, last = (1, 2, 3, 4, 5) # first = 1, middle = [2, 3, 4], last = 5

The starred expression *middle receives every element between the first and last positions. The result is always a list, even when the source is a tuple. This behavior is consistent across all iterables: tuples, lists, strings, and generators all produce a list for the starred target.

How the Starred Target Behaves

The starred expression can appear at the beginning, middle, or end of the assignment target list, but Python permits only one starred target per assignment:

python
head, *tail = (1, 2, 3, 4) # head = 1, tail = [2, 3, 4] *first_two, last = (1, 2, 3, 4) # first_two = [1, 2], last = 4

When the starred target is in the middle, Python first assigns the non-starred targets from the ends and then assigns all remaining elements to the starred target. If the iterable has exactly enough elements for the fixed targets, the starred target receives an empty list rather than raising an error.

Practical Use Cases

Starred unpacking is useful when you need to separate data into a fixed prefix or suffix and a variable middle. Common scenarios include:

  • Parsing a configuration line where the first field is a key and the rest are values
  • Splitting a list into a head and tail for recursive algorithms
  • Extracting the first and last elements while ignoring the middle
  • Expanding an iterable into positional function arguments with *
python
def process_record(record): record_id, *fields = record # record_id is the first element, fields holds the rest

This pattern removes the need for slice arithmetic and index tracking, making the code's intent explicit.

Common Errors and How to Avoid Them

The most frequent mistake is using more than one starred expression in a single assignment:

python
first, *middle, second_last, *last = (1, 2, 3, 4, 5) # SyntaxError

Python raises a SyntaxError because the grammar does not allow two variable-length targets in one assignment. If you need two variable-length groups, restructure the logic: unpack once, then apply further unpacking to the result.

Another error occurs when the source iterable has fewer elements than fixed targets:

python
a, b, *rest = (1,) # ValueError: not enough values to unpack

The starred target does not compensate for missing fixed targets. Each fixed target must receive exactly one element.

Starred Unpacking with Function Arguments

The same * syntax appears in function calls, but it serves a different purpose. When you write func(*args), Python expands the iterable into positional arguments. This pairs naturally with tuple unpacking:

python
def calculate(x, y, z): return x + y * z point = (2, 3, 4) result = calculate(*point) # calculate(2, 3, 4)

This is not the same as starred assignment unpacking, but the two patterns are often used together. Understanding the distinction prevents confusion when reading code that mixes both.

Performance and Maintainability Considerations

The starred target always allocates a new list, so unpacking a very large iterable into a starred target copies every remaining element. For most applications this cost is irrelevant. If you are unpacking millions of elements, consider whether you need the materialized list or can iterate directly.

From a maintainability perspective, starred unpacking improves readability when the structure of the data is stable. It removes the need for index arithmetic and slice expressions:

python
# Without starred unpacking first = data[0] middle = data[1:-1] last = data[-1] # With starred unpacking first, *middle, last = data

The starred version communicates intent more clearly. However, if the sequence length varies unpredictably, the fixed targets will raise ValueError, so validate input before unpacking when the data comes from an external source.

When to Choose Alternatives

Starred unpacking is not always the right tool. For deeply nested structures, explicit indexing or operator.itemgetter may be clearer. For dictionaries, ** expands key-value pairs in calls and dict literals, which is a different mechanism entirely. If you need to discard the middle values without keeping a named reference, assign to a throwaway name:

python
first, *_, last = data

The underscore convention signals that the middle values are intentionally ignored, and the list is still allocated but immediately eligible for garbage collection.

When the sequence structure is stable and the variable-length portion is clearly identifiable, starred unpacking is the most direct way to express the assignment. The rule to remember is simple: at most one starred target per assignment, and that target always receives a list regardless of the source iterable type.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.