0
0
PythonComparisonBeginner · 3 min read

Stack vs Queue in Python: Key Differences and Usage

A stack in Python is a data structure that follows Last In First Out (LIFO) order, where the last added item is removed first. A queue follows First In First Out (FIFO) order, where the first added item is removed first. Both can be implemented using lists or collections modules but serve different purposes based on their order of element access.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of stack and queue in Python based on key factors.

FactorStackQueue
OrderLast In First Out (LIFO)First In First Out (FIFO)
Main Operationspush (add), pop (remove)enqueue (add), dequeue (remove)
Common Use CaseUndo functionality, backtrackingTask scheduling, buffering
Python ImplementationList with append() and pop()collections.deque with append() and popleft()
Access PatternAccess only the top elementAccess only the front element
PerformanceFast for adding/removing at endFast for adding/removing at both ends with deque
⚖️

Key Differences

A stack is a data structure where the last element added is the first one to be removed. This is called Last In First Out (LIFO). Imagine a stack of plates: you add plates on top and remove the top plate first. In Python, you can use a list and add items with append() and remove with pop() to simulate a stack.

A queue works differently. It follows First In First Out (FIFO), meaning the first element added is the first to be removed. Think of a line at a store: the first person to get in line is the first to be served. Python’s collections.deque is ideal for queues because it allows fast adding and removing from both ends using append() and popleft().

While both structures store items, their order of access and typical use cases differ. Stacks are great for undo features or backtracking algorithms, while queues are used for scheduling tasks or managing data streams.

⚖️

Code Comparison

Here is how you can implement a stack in Python to add and remove items in LIFO order.

python
stack = []

# Add items (push)
stack.append('apple')
stack.append('banana')
stack.append('cherry')

print('Stack after pushes:', stack)

# Remove items (pop)
item = stack.pop()
print('Popped item:', item)
print('Stack after pop:', stack)
Output
Stack after pushes: ['apple', 'banana', 'cherry'] Popped item: cherry Stack after pop: ['apple', 'banana']
↔️

Queue Equivalent

Here is how you can implement a queue in Python using collections.deque to add and remove items in FIFO order.

python
from collections import deque

queue = deque()

# Add items (enqueue)
queue.append('apple')
queue.append('banana')
queue.append('cherry')

print('Queue after enqueues:', list(queue))

# Remove items (dequeue)
item = queue.popleft()
print('Dequeued item:', item)
print('Queue after dequeue:', list(queue))
Output
Queue after enqueues: ['apple', 'banana', 'cherry'] Dequeued item: apple Queue after dequeue: ['banana', 'cherry']
🎯

When to Use Which

Choose a stack when you need to reverse order or track recent actions, such as undo features or parsing expressions. Use a queue when order matters and you want to process items in the order they arrive, like task scheduling or handling requests.

Stacks are simple and efficient for last-in-first-out needs, while queues handle first-in-first-out scenarios better, especially with deque for performance.

Key Takeaways

Stacks follow LIFO order; queues follow FIFO order.
Use Python lists for stacks and collections.deque for queues.
Stacks are ideal for undo and backtracking tasks.
Queues are best for scheduling and processing in arrival order.
Choosing the right structure improves code clarity and performance.