Heap Functions

heapq.heapreplace()

Pop the existing smallest item and then push the new item. Unlike heappushpop, it can return a value greater than the new item; empty heaps raise IndexError.

Formal Syntax & Structure

python syntaxpython
heapq.heapreplace(heap, item, /)

Parameters & Attributes

NameTypeDescription
heaplistMutable list satisfying the min-heap invariant unless heapifying.
itemobjectComparable item to add.

Return Value

object

The object result described above.

Working Example

Example Snippetpython
import heapqheap=[2,3]print((heapq.heapreplace(heap,1),sorted(heap)))
Python Reference