On this page
heapq.heapreplace
Prev: heapq.heappushpop_max | Up: heapq | Next: heapq.heapreplace_max
Rel: heapq.heapify, heapq.heapify_max, heapq.heappop, heapq.heappop_max
Ref: docs.python.org — heapq.heapreplace
Tags: #public #python3 #stdlib #heapq
Import
import heapq
from heapq import heapreplace
Signature & Overview
heapq.heapreplace(heap, item, /)
Pop and return the current smallest value, and add the new item.
Usage
import heapq
h = [2, 5, 8]
heapq.heapify(h)
assert heapq.heapreplace(h, 1) == 2 # pops BEFORE pushing (so 1 stays in heap)
assert h[0] == 1
Gotchas
- Pops first, then pushes: Unlike
heappushpop(),heapreplace()always returns the previous smallest item and raisesIndexErrorifhis empty.
REPL Inspect
from heapq import heapreplace
help(heapreplace)
Linked from
Related notes
- tuts/python3/stdlib_module/heapq/heappushpop_max Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapreplace_max Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/heapq/heapify Linked here; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapify_max Linked here; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappop Linked here; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappop_max Linked here; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappush shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappush_max shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappushpop shared tags: #heapq, #stdlib