heapq.heappush()
heapq.heappush() is a Python function in the Heapq Module category. Pushes an item onto the heap, maintaining the heap invariant. The syntax is heapq.heappush(heap, item). Parameters: heap, item. Returns: None. A typical example: h = []
heapq.heappush(h, 5)
heapq.heappush(h, 1)
heapq.heappush(h, 3)
# h = [1, 5, 3]. 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.heappop(), which pops and returns the smallest item from the heap. A close sibling is heapq.heappushpop(), which pushes item then pops the smallest. More efficient than heappush then heappop. 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.heappop(), heapq.heappushpop(), 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.