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
| Name | Type | Description |
|---|---|---|
| heap | list | Mutable list satisfying the min-heap invariant unless heapifying. |
| item | object | Comparable 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)))