Heap Functions

heapq.heappush()

Add an item to a min-heap while preserving the invariant. Items must be mutually comparable.

Formal Syntax & Structure

python syntaxpython
heapq.heappush(heap, item, /)

Parameters & Attributes

NameTypeDescription
heaplistMutable list satisfying the min-heap invariant unless heapifying.
itemobjectComparable item to add.

Return Value

None

No return value; the operation changes the receiver in place.

Working Example

Example Snippetpython
import heapqheap=[2,3]heapq.heappush(heap,1)print((heap[0],sorted(heap)))
Python Reference