Stacks and queues in Python - Python Morsels
Stacks and queues in Python
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).
Trey Hunner
June 10, 2026
3 min read
03:16 video
Python 3.10—3.14
Watch this as a 03:16 screencast
Same content, narrated. Your preference is remembered for next time.
Watch it
Show captions
Autoplay
Hide video
Sign in to change your settings
Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
Let's talk about stacks and queues , and how to use them in Python.
Stacks versus Queues
In Computer Science, stacks and queues are data structures that are optimized to make it inexpensive to remove either the most recently added item or the least recently added item.
A queue is often called a FIFO data structure: first in, first out .
You can think of a queue as... well, a queue.<br>Or a "line", for Americans like me.<br>The first person to enter a queue will be the first person to reach the front of the queue.
And in programming queues, the first item added will be the first item removed .
A stack is often called a LIFO data structure: last in, first out .
You can think of a stack as a stack of plates... specifically one of those spring-loaded ones from a self-service lunch counter.<br>The last plate that's added to the top of the stack will be the first plate removed from the top of the stack.
And in programming stacks, the last item added will be the first item that's removed .
But how do these terms apply to Python?
Stacks in Python
You can think of Python lists as stack-like data structures.
Lists are optimized for adding items to the end and removing items from the end:
>>> numbers = []
Let's append a bunch of items to a list one-by-one:
>>> for n in range(5):<br>... numbers.append(n)<br>>>> numbers<br>[0, 1, 2, 3, 4]
Note that the first item that was added will be the last item that's removed if we call the list.pop() method:
>>> while numbers:<br>... print(numbers.pop())
That's last in, first out, which is like a stack.
Now, I should note that there's nothing stopping us from inserting an item somewhere else in the list, like at the beginning:
>>> numbers = [1, 3, 4, 7, 11, 18]<br>>>> numbers.insert(0, 2)<br>>>> numbers<br>[2, 1, 3, 4, 7, 11, 18]
And there's nothing stopping us from removing an item from the beginning (or any other position) :
>>> numbers.pop(0)<br>>>> numbers<br>[1, 3, 4, 7, 11, 18]
But these operations are not what lists are optimized for.
This code inserts new list items repeatedly 200,000 times:
>>> numbers = []<br>>>> for n in range(200_000):<br>... numbers.insert(0, n)<br>...
And it takes a few seconds to run.
But if we run the same code by appending items to the end, our code runs much faster:
>>> numbers = []<br>>>> for n in range(200_000):<br>... numbers.append(n)<br>...
So if you need a stack-like structure in Python, use a list.
Queues in Python
What if you want a queue-like structure?
Well, Python's collections module has a deque class that can work like a queue:
>>> from collections import deque<br>>>> queue = deque()
Let's append items to this deque item one-by-one:
>>> for n in range(5):<br>... queue.append(n)<br>>>> queue<br>deque([0, 1, 2, 3, 4])
Note that these items are added to the right-hand side (the end) of our deque.
If we then use the popleft method to repeatedly remove items, the first item that was added will be the first item that was removed:
>>> while queue:<br>... print(queue.popleft())
That's first in, first out, which is like a queue.
A deque is a "double-ended queue"
The name deque stands for "double-ended queue", meaning deque objects are actually a sort of hybrid structure: they can be used as a stack, used as a queue, or used as both :
>>> from collections import deque<br>>>> queue = deque([1, 3, 4, 7, 11])
So we can add items to the right-hand side of our queue, or we can add items to the left-hand side:
>>> queue.append(18) # Add to right<br>>>> queue.appendleft(2) # Add to left<br>>>> queue<br>deque([2, 1, 3, 4, 7, 11, 18])
And we can also remove items from the right-hand side, or remove items from the left-hand side:
>>> queue.pop() # Remove from right<br>18<br>>>> queue.popleft() # Remove from left<br>>>> queue<br>deque([1, 3, 4, 7, 11])
Stack-like and queue-like operations
In Python, lists can be efficiently used for stack-like operations.
If you need something that acts like a stack, use a list.<br>But if you need something that acts like a queue, use a deque (also known as a double-ended queue) from Python's collections module.
A Python tip every week
Need to fill-in gaps in your Python skills?
Sign up for my Python newsletter where I share one of my favorite Python tips every week .
Website
Get Trey's Python tips
On this page
Stacks versus Queues
Stacks in Python
Queues in Python
A deque is a "double-ended queue"
Stack-like and queue-like operations
Add to queue
Sign up free to track...