heapq.heappop() vs heapq.heappushpop()
heapq.heappop() and heapq.heappushpop() are both Python Heapq Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. heapq.heappop(): Pops and returns the smallest item from the heap. Its syntax is heapq.heappop(heap), and it returns smallest item heapq.heappushpop(): Pushes item then pops the smallest. More efficient than heappush then heappop. Its syntax is heapq.heappushpop(heap, item), and it returns smallest item Example of heapq.heappop(): h = [1, 3, 5, 7]
heapq.heappop(h) # 1
heapq.heappop(h) # 3. Example of heapq.heappushpop(): h = [3, 5, 7]
heapq.heappushpop(h, 1) # Pops 1, pushes 1. Read the full references for heapq.heappop() and heapq.heappushpop() to see parameters, edge cases, and example code side by side. Other Heapq Module APIs worth comparing: heapq.heapify(), heapq.heappush(), heapq.heapreplace(), heapq.nlargest(), heapq.nsmallest(). Both belong to the Heapq Module category in the Python Reference documentation.