On this page
heapq.heappush
Prev: heapq.heappop_max | Up: heapq | Next: heapq.heappush_max
Rel: heapq.heapify, heapq.heapify_max, heapq.heappop, heapq.heappop_max
Ref: docs.python.org — heapq.heappush
Tags: #public #python3 #stdlib #heapq
Import
import heapq
from heapq import heappush
Signature & Overview
heapq.heappush(heap, item, /)
Push item onto heap, maintaining the heap invariant.
Usage
import heapq
h: list[int] = []
for v in (4, 1, 7):
heapq.heappush(h, v)
assert h[0] == 1
Gotchas
- $O(\log n)$ push: For
(priority, payload)tuples where priorities can tie, include a tie-breaker counter(priority, i, payload)so non-comparable payloads don't raiseTypeError.
REPL Inspect
from heapq import heappush
help(heappush)
Linked from
Related notes
- tuts/python3/stdlib_module/heapq/heapify Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapify_max Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappop Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappop_max Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappush_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/heappushpop shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappushpop_max shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapreplace shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapreplace_max shared tags: #heapq, #stdlib