On this page
  1. heapq.heapreplace
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from heapq import heapreplace
help(heapreplace)

Linked from

Related notes