Python Heapq Module
Heap queue algorithm — heapify, heappush, heappop, nlargest. This category documents 7 Python heapq module entries, and each page gives you the full syntax, the parameter list, the return value, and a runnable example. The entries include: heapq.heapify(), heapq.heappush(), heapq.heappop(), heapq.heappushpop(), heapq.heapreplace(), heapq.nlargest(), heapq.nsmallest(). heapq.heapify() transforms a list into a heap (min-heap), in-place, O(n) time. heapq.heappush() pushes an item onto the heap, maintaining the heap invariant. heapq.heappop() pops and returns the smallest item from the heap. heapq.heappushpop() pushes item then pops the smallest. More efficient than heappush then heappop. heapq.heapreplace() pops then pushes — more efficient than heappop then heappush. Heapq Module belongs to the Collections & Iteration group alongside Collections Module, Itertools Module, Functools Module, Operator Module. You can also search across every category from the search page, browse the complete A–Z index, or try any example in the Python playground.