heapq

Source code: heapq.seq

heapq.heappush[T](heap: list[T], item: T)

Push item onto heap, maintaining the heap invariant.

heapq.heappop[T](heap: list[T])

Pop the smallest item off the heap, maintaining the heap invariant.

heapq.heapreplace[T](heap: list[T], item: T)

Pop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement: if item > heap[0]: item = heapreplace(heap, item).

heapq.heappushpop[T](heap: list[T], item: T)

Fast version of a heappush followed by a heappop.

heapq.heapify[T](x: list[T])

Transform list into a heap, in-place, in $O(len(x))$ time.

heapq.nsmallest[S, T](n: int, iterable: generator[T], key: optional[function[S, T]] = None)

Find the n smallest elements in a dataset. Equivalent to: sorted(iterable, key=key)[:n]

heapq.nlargest[S, T](n: int, iterable: generator[T], key: optional[function[S, T]] = None)

Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n]