Python & Django team trainer
I help folks sharpen their Python skills with https://PythonMorsels.com ππͺ
YIMBY. 95% vegan.
Loading...
Loading...
New screencast on stack-like and queue-like operations in Python.
Lists are great for one of these and not so great for the other.
Trey Hunner
πΊοΈ June 10, 2026
5/5 countries in 6/13 guesses
π’π’π’π‘π’
whereabouts.earth/daily/
#Whereabouts
πΊοΈ June 9, 2026
5/5 countries in 5/13 guesses
π’π’π’π’π’
whereabouts.earth/daily/
#Whereabouts
The rivers helped me a lot today
You don't need to replace ALL your lists with deques. Lists are still the right default: they're great for indexing, slicing, and stack-like operations.
But if you find yourself frequently adding or removing items from the beginning, a deque is a better fit than a list.
To remove a prefix or suffix, use the removeprefix or removesuffix methods.
Read more π https://trey.io/dley4p
#Python
Python Tip #160 (of 365):
Using list.insert(0, ...)? Consider using a deque instead. π§΅
Inserting at the beginning of a list is slow because every existing item needs to be shuffled over.
This takes seconds:
nums = []
for n in range(200_000):
nums.insert(0, n)
#Python #DailyPythonTip
βGenerator expressions also pair nicely with reducer functions.β
Read more π pym.dev/custom-compr...
#Python
But this runs nearly instantly:
from collections import deque
numbers = deque()
for n in range(200_000):
numbers.appendleft(n)
A deque (short for "double-ended queue") is optimized for both ends. It has appendleft and popleft in addition to the usual append and pop.
Use a Python list for stack operations (last-in, first-out) and a deque from the collections module for queue operations (first-in, first-out).