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
| Name | Type | Description |
|---|---|---|
| heap | list | Mutable list satisfying the min-heap invariant unless heapifying. |
| item | object | Comparable 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)))