Skip to content

Glossary: Data Structures and Algorithms

heapq

Also written: heappush, heappop, heapify, nlargest, nsmallest

A Python module that treats an ordinary list as a min-heap. `heappush` and `heappop` are O(log n), `heapify` rearranges an existing list in O(n), and `nlargest`/`nsmallest` return the top few items.

Where the book teaches it

Related terms

HeapPriority queueMax-heap