heapq

Stdlib — DataPython 2.0+Intermediate

Heap queue algorithm (priority queue) on plain lists

Quick Info

Documentation
Official Docs
Python Version
2.0+
Dependencies
None — Python Standard Library
Install
Included with Python

Learn by Difficulty

Quick Example

python
import heapq

# Min-heap operations
nums = [5, 1, 8, 3, 9, 2, 7]
heapq.heapify(nums)
print(f"Heap: {nums}")
print(f"Smallest: {heapq.heappop(nums)}")
print(f"3 largest: {heapq.nlargest(3, [5, 1, 8, 3, 9])}")

The heapq module is part of Python's standard library. Heap queue algorithm (priority queue) on plain lists.

Try in Playground

Tags

stdlibdata-structure