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

heapq.heappushpop

Prev: heapq.heappush_max | Up: heapq | Next: heapq.heappushpop_max
Rel: heapq.heapify, heapq.heapify_max, heapq.heappop, heapq.heappop_max
Ref: docs.python.org — heapq.heappushpop
Tags: #public #python3 #stdlib #heapq


Import

import heapq
from heapq import heappushpop

Signature & Overview

heapq.heappushpop(heap, item, /)

Push item on the heap, then pop and return the smallest item from the heap.

Usage

import heapq

h = [2, 5, 8]
heapq.heapify(h)
assert heapq.heappushpop(h, 1) == 1   # pushed 1 is immediately popped
assert heapq.heappushpop(h, 4) == 2   # pops 2, keeps 4

Gotchas

REPL Inspect

from heapq import heappushpop
help(heappushpop)

Linked from

Related notes