Python heapq.heappop() Method
Last Updated :
12 Mar, 2025
The heapq.heappop() function in Python is used to pop and return the smallest element from a heap, maintaining the heap property. This function is extremely useful in situations where the smallest element in a min-heap needs to be processed first, such as in priority queues or sorting algorithms.
Example:
Python
import heapq
# Create a heap
h = [1, 3, 5, 7, 9, 2]
# Convert the list into a heap
heapq.heapify(h)
# Pop the smallest element
heapq.heappop(h)
print("Heap after pop:", h)
OutputHeap after pop: [2, 3, 5, 7, 9]
Explanation:
- heapq.heapify(heap) converts the list into a min-heap.
- heapq.heappop(heap) removes the smallest element (root) of the heap.
Syntax of heappop() method
heapq.heappop(heap)
Parameters
- heap: The list representing the heap (must be a valid heap structure).
Return Value
The heapq.heappop() function removes and returns the smallest element from the heap. After the operation, the heap is automatically rearranged to maintain the heap property.
Examples of heappop() method
1. Using heappop() to Remove the Smallest Element from a Min-Heap
Python
import heapq
# Create a heap and push elements
h = []
heapq.heappush(h, 5)
heapq.heappush(h, 1)
heapq.heappush(h, 8)
heapq.heappush(h, 3)
# Pop the smallest element
smallest = heapq.heappop(h)
print("Smallest element:", smallest)
print("Heap after pop:", h)
OutputSmallest element: 1
Heap after pop: [3, 5, 8]
Explanation:
- We push elements 5, 1, 8, and 3 into the heap using heapq.heappush(). The heap automatically maintains the min-heap property, where the smallest element is always at index 0.
- heapq.heappop() is used to pop the smallest element from the heap, which is 1 in this case.
- After popping, the heap is rearranged to maintain the heap property. The heap becomes [3, 5, 8].
2. Using heappop() in a Priority Queue
A priority queue can be implemented using a heap, where elements are processed based on their priority. The smallest element (highest priority) is always popped first.
Python
import heapq
# List of tuples (priority, task)
pq = []
# Push elements (priority, task)
heapq.heappush(pq, (2, "Task A"))
heapq.heappush(pq, (1, "Task B"))
heapq.heappush(pq, (3, "Task C"))
# Pop the highest priority task
priority, task = heapq.heappop(pq)
print("Highest priority task:", task)
OutputHighest priority task: Task B
Explanation:
- A list pq is used to store tuples with (priority, task).
- The heappush() function is used to insert tasks with their priority values into the heap.
- The task with the highest priority (smallest priority value) is popped using heappop().
- The task with the highest priority is printed.
3. Simulating a Max-Heap with heappop()
Since heapq only supports min-heaps, we can simulate a max-heap by negating the values when pushing and popping elements.
Python
import heapq
h = []
# Push elements as negative values to simulate max-heap
heapq.heappush(h, -5)
heapq.heappush(h, -1)
heapq.heappush(h, -8)
heapq.heappush(h, -3)
# Pop the largest element (simulated by negating)
largest = -heapq.heappop(h)
print("Largest element:", largest)
print("Heap after pop:", [-x for x in h])
OutputLargest element: 8
Heap after pop: [5, 3, 1]
Explanation:
- Elements are pushed onto the heap as negative values to simulate a max-heap (since heapq is a min-heap).
- The largest element is popped from the heap using heappop(), but since the elements were negated, we negate it again to get the original value.