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.

Starred tuple unpacking uses the * operator during assignment to capture a variable number of elements from an iterable. The syntax is easy to recognize:
first, *middle, last = (1, 2, 3, 4, 5)
# first = 1, middle = [2, 3, 4], last = 5The 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:
head, *tail = (1, 2, 3, 4) # head = 1, tail = [2, 3, 4]
*first_two, last = (1, 2, 3, 4) # first_two = [1, 2], last = 4When 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
*
def process_record(record):
record_id, *fields = record
# record_id is the first element, fields holds the restThis 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:
first, *middle, second_last, *last = (1, 2, 3, 4, 5) # SyntaxErrorPython 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:
a, b, *rest = (1,) # ValueError: not enough values to unpackThe 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:
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:
# Without starred unpacking
first = data[0]
middle = data[1:-1]
last = data[-1]
# With starred unpacking
first, *middle, last = dataThe 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:
first, *_, last = dataThe 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 toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.