Heap Functions

heapq.heapify()

Rearrange a list in place into a min-heap in linear time. The root is smallest; the list need not be globally sorted.

Formal Syntax & Structure

python syntaxpython
heapq.heapify(heap, /)

Parameters & Attributes

NameTypeDescription
heaplistMutable list satisfying the min-heap invariant unless heapifying.

Return Value

None

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

Working Example

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