Heap Modules

heapq

Min-heap algorithms for managing priorities in ordinary lists. The smallest element is at index zero; repeated pops yield sorted order without requiring the entire list to remain sorted.

Formal Syntax & Structure

python syntaxpython
heapq

Return Value

module

Module object available through import.

Working Example

Example Snippetpython
import heapqh=[3,1,2]; heapq.heapify(h)print([heapq.heappop(h) for _ in range(3)])
Python Reference