heapq.heappushpop()
heapq.heappushpop() is a Python function in the Heapq Module category. Pushes item then pops the smallest. More efficient than heappush then heappop. The syntax is heapq.heappushpop(heap, item). Returns: Smallest item. A typical example: h = [3, 5, 7]
heapq.heappushpop(h, 1) # Pops 1, pushes 1. A close sibling is heapq.heapify(), which transforms a list into a heap (min-heap), in-place, O(n) time. A close sibling is heapq.heappush(), which pushes an item onto the heap, maintaining the heap invariant. A close sibling is heapq.heappop(), which pops and returns the smallest item from the heap. A close sibling is heapq.heapreplace(), which pops then pushes — more efficient than heappop then heappush. More about this category: Heap queue algorithm — heapify, heappush, heappop, nlargest. Related Heapq Module entries: heapq.heapify(), heapq.heappush(), heapq.heappop(), heapq.heapreplace(), heapq.nlargest(), heapq.nsmallest(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.