Stack vs Queue in Python: Key Differences and Usage
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.
| Factor | Stack | Queue |
|---|---|---|
| Order | Last In First Out (LIFO) | First In First Out (FIFO) |
| Main Operations | push (add), pop (remove) | enqueue (add), dequeue (remove) |
| Common Use Case | Undo functionality, backtracking | Task scheduling, buffering |
| Python Implementation | List with append() and pop() | collections.deque with append() and popleft() |
| Access Pattern | Access only the top element | Access only the front element |
| Performance | Fast for adding/removing at end | Fast 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.
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)
Queue Equivalent
Here is how you can implement a queue in Python using collections.deque to add and remove items in FIFO order.
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))
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.