Heap and Priority Queue in Python: A Complete Guide with Examples
#Heap Priority Queue in Python: A Complete Guide with Examples
When working with large datasets or scheduling tasks, Heaps and Priority Queues are powerful tools in Python. They allow you to efficiently manage elements based on priority instead of just the order of insertion. In this guide, we’ll explore heapq module, priority queues, their differences, and practical examples.
A Heap is a specialized tree-based data structure that maintains the heap property:
Min-Heap: The parent node is always smaller than or equal to its children.
Max-Heap: The parent node is always greater than or equal to its children.
In Python, the heapq
module provides an efficient implementation of Min-Heap.
heapq
import heapq
# Create a list
numbers = [20, 15, 30, 5, 10]
# Convert list into a heap
heapq.heapify(numbers)
print("Min-Heap:", numbers)
# Push new element
heapq.heappush(numbers, 2)
print("After Push:", numbers)
# Pop the smallest element
print("Popped:", heapq.heappop(numbers))
print("Final Heap:", numbers)
📌 Output:
Min-Heap: [5, 10, 30, 20, 15]
After Push: [2, 10, 5, 20, 15, 30]
Popped: 2
Final Heap: [5, 10, 30, 20, 15]
A Priority Queue is a data structure where each element is assigned a priority, and the highest priority element is dequeued first.
Unlike normal queues (FIFO), priority queues focus on priority-based ordering.
Python offers multiple ways to implement priority queues:
Using heapq
(lightweight and fast)
Using queue.PriorityQueue
(thread-safe)
Using sortedcontainers
or manual sorting (less efficient)
heapq
import heapq
# List of (priority, task)
tasks = [(2, "Write blog"), (1, "Fix bug"), (3, "Read book")]
# Convert into a heap
heapq.heapify(tasks)
while tasks:
priority, task = heapq.heappop(tasks)
print(f"Task: {task}, Priority: {priority}")
📌 Output:
Task: Fix bug, Priority: 1
Task: Write blog, Priority: 2
Task: Read book, Priority: 3
queue.PriorityQueue
from queue import PriorityQueue
pq = PriorityQueue()
pq.put((2, "Write blog"))
pq.put((1, "Fix bug"))
pq.put((3, "Read book"))
while not pq.empty():
priority, task = pq.get()
print(f"Task: {task}, Priority: {priority}")
This implementation is thread-safe, making it ideal for multi-threaded applications.
Feature | Heap (heapq ) | Priority Queue (PriorityQueue ) |
---|---|---|
Thread Safety | ❌ Not thread-safe | ✅ Thread-safe |
Performance | ✅ Faster (lightweight) | ⚠️ Slightly slower |
Common Use Cases | Algorithms, single-thread | Multi-threaded applications |
Dijkstra’s shortest path algorithm (graph traversal)
Job scheduling (CPU task management)
Event-driven simulations
Bandwidth or resource management
Use heapq
for performance in single-threaded applications.
Use PriorityQueue
when working with multi-threaded programs.
Always store priority as the first element of a tuple for proper ordering.
For max-heap, store negative values since Python’s heapq
is min-heap by default.
Both Heap and Priority Queue in Python provide efficient ways to handle data when priority matters more than insertion order. The heapq
module is great for lightweight operations, while PriorityQueue
ensures thread safety in concurrent environments.
By mastering these data structures, Python developers can write efficient, scalable, and reliable programs for scheduling, optimization, and graph-related problems.