Quick look at Python 3’s Stared Expressions

NeuroWinter
3 min readNov 21, 2021

A starred expression is when you add a “*” in front of a variable. It can also be called “Extended Iterable Unpacking”

This has the effect of acting as a “catch all” for lists.

The idea behind this is to replace this “messy” line:

first, rest = li[0], li[1:]

It can be replaced with:

first, *rest = li

One interesting use-case is capturing both the start and the end of a list, and the list without those indexes:

li                  = [1, 2, 3, 4, 5]
start, *middle, end = li
print(start) # outputs 1
print(middle) # outputs [2, 3, 4]
print(end) # outputs 5

Comparison between Starred Expressions and Indexing

Now lets have a look at the differences in the speed of each approach — Index vs starred expression.

Below is a graph showing the time it takes to complete 1000000 loops of each of the following snippets:

Normal Indexing

start=lst[0]
middle=lst[1:-1]
end=lst[-1]

Stared Expressions

start, *middle, end = lst

Where lst is a list of integers from 1 -> n.

As you can see the Starred expression is a fair bit slower than using normal indexing. This is because the Starred expression does the following:

Collect all items for mandatory targets before the starred one

Collect all remaining items from the iterable in a list

Pop items for mandatory targets after the starred one from the list

Push the single items and the resized list on the stack

Source: https://www.python.org/dev/peps/pep-3132/#changes-to-the-bytecode-interpreter

It is also important to note that while I have only talked about lists in this post, the expression can be used on any iterable, for example Sets where the index approach will not work:

m = set((1,2,3,4))[1:-1]
Traceback (most recent call last):
File "<input>", line 1, in <module>
m = set((1,2,3,4))[1:-1]
TypeError

--

--

NeuroWinter

A collection of my learnings in all things Python, AI, MLOPS, NLP, Software engineering, and Bug Bounty Hunting.