![]() |
VOOZH | about |
A heap queue (also called a priority queue) is a data structure that allows quick access to the smallest (min-heap) or largest (max-heap) element.
Example: Converting a normal list into a heap using heapify().
Heap queue: [15, 20, 25, 30, 40]
Explanation: heapq.heapify(li) rearranges the list into a heap where the smallest element is at the root. It does not fully sort the list.
Heaps support several essential operations that help manage data efficiently while maintaining heap property.
| Operation | Function | Description |
|---|---|---|
| Create | heapq.heapify() | Converts a regular list into a valid min-heap |
| Push | heapq.heappush() | Adds a new element to the heap while maintaining the heap property |
| Pop | heapq.heappop() | Removes and returns the smallest element from the heap |
| Peek | heap[0] | Accesses the smallest element without removing it |
| Push and Pop | heapq.heappushpop() | Pushes a new element and removes the smallest element in one step |
| Replace | heapq.heapreplace() | Removes the smallest element and inserts a new element in one operation |
By default, Python's heapq implements a min-heap. To create a max-heap simply invert the values (store negative numbers).
Example: Below example, convert a list into a max-heap by storing negative numbers and then retrieve the largest element:
Largest element: 40
Explanation: We store negative values so that the smallest (negative largest) is treated as root. When retrieving values, we multiply by -1 again to restore the original numbers.
Elements can be inserted and removed while maintaining the heap property:
Example: This code demonstrates how to create a heap, append an element and remove the smallest element.
[5, 20, 10, 30, 40, 15] Smallest: 5 [10, 20, 15, 30, 40]
Explanation:
heapq.heappushpop() pushes a new element onto the heap and removes the smallest element in a single operation.
Example: Pushes 5 onto the heap and pops the smallest element in a single step using heappushpop().
5 [10, 20, 15, 30, 40]
Explanation: heappushpop(h, 5) first pushes 5 into the heap and immediately pops the smallest element (which is also 5).
heapq.nlargest() and heapq.nsmallest() return the required number of largest or smallest elements from an iterable.
Example: Finding the 3 largest and 3 smallest elements using nlargest() and nsmallest()
3 largest elements: [40, 30, 20] 3 smallest elements: [10, 15, 20]
Note: The heapq module allows in-place heap operations on lists, making it an efficient and simple way to implement priority queues and similar structures.
heapq.heapreplace() removes the smallest element and inserts a new one in a single step, returning the removed value. While, heapq.merge() combines multiple sorted iterables into a single sorted sequence.
Example: This example replaces the smallest element in a heap and then merges it with another sorted list.
10 [5, 20, 15, 30, 40] Merged heap: [2, 4, 5, 6, 8, 15, 20, 30, 40]
Explanation:
| Advantages | Disadvantages |
|---|---|
| Fast for insertion and removal with priority. | Not suitable for complex data manipulations |
| Uses less memory than some other data types. | No direct access to middle items. |
| Simple to use with the heapq module. | Can’t fully sort the items automatically. |
| Works in many cases like heaps and priority queues. | Not safe with multiple threads at the same time. |