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[T](n : int, iterable : Generator[T], key = Optional[int]())
Find the n smallest elements in a dataset.

Equivalent to: sorted(iterable, key=key)[:n]

heapq.nlargest[T](n : int, iterable : Generator[T], key = Optional[int]())
Find the n largest elements in a dataset.

Equivalent to: sorted(iterable, key=key, reverse=True)[:n]