Python heapq keeps a list as a min-heap for priority queues and n-smallest queries
Answer: Start from [] or call heapify(x), then use heappush(heap, item) and heappop(heap); the smallest element is always the root, heap[0]. For a priority queue, push (priority, count, task) entries so the entry count acts as a tie-breaker.
Example
import heapq, itertools
heap = []
counter = itertools.count()
heapq.heappush(heap, (2, next(counter), "write docs"))
heapq.heappush(heap, (1, next(counter), "fix bug"))
priority, _, task = heapq.heappop(heap) # (1, ..., "fix bug")
nums = [5, 1, 8, 3]
heapq.heapify(nums) # in place, linear time
heapq.nlargest(2, [5, 1, 8, 3]) # [8, 5]
list(heapq.merge([1, 4], [2, 3])) # [1, 2, 3, 4]
Functions
heappopraisesIndexErrorif the heap is empty; useheap[0]to see the smallest item without popping it.heappushpop(heap, item)pushes then pops, which runs more efficiently than separate calls; it returns the smaller of the two values.nlargest(n, iterable, key=None)is equivalent tosorted(iterable, key=key, reverse=True)[:n], andnsmallesttosorted(iterable, key=key)[:n].- Python 3.14 added max-heap versions with a
_maxsuffix, such asheapify_max,heappush_maxandheappop_max.
How it works
Min-heaps are binary trees where every parent node has a value less than or equal to any of its children. heapq uses zero-based lists with heap[k] <= heap[2k+1] and heap[k] <= heap[2k+2], so heap.sort() maintains the heap invariant.
Why the entry count
With plain (priority, task) pairs, tuple comparison breaks when priorities are equal and the tasks do not have a default comparison order.
See also
Claims
- id: C1 claim: The smallest element of a min-heap is always the root. source: S1 evidence: "its smallest element is always the root"
- id: C2 claim: heapify transforms a list into a min-heap in linear time. source: S1 evidence: "Transform list x into a min-heap, in-place, in linear time."
- id: C3 claim: The entry count serves as a tie-breaker. source: S1 evidence: "The entry count serves as a tie-breaker"
- id: C4 claim: heappop pops the smallest item from the heap. source: S2 evidence: "# pops the smallest item from the heap"
Sources
- [S1] heapq — Heap queue algorithm. Python docs. https://docs.python.org/3/library/heapq.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/heapq.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/heapq.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, heapq — Heap queue algorithm; python/cpython (GitHub), Lib/heapq.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, heapq — Heap queue algorithm; python/cpython (GitHub), Lib/heapq.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python heapq keeps a list as a min-heap for priority queues and n-smallest queries." iinventedlight Library, iil:note:zwsx3mfn, version of 2026-11-18. https://iinventedlight.com/library/notes/python-heapq/. License: CC-BY-4.0.
@misc{iil_note_zwsx3mfn,
author = {{iinventedlight}},
title = {{Python heapq keeps a list as a min-heap for priority queues and n-smallest queries}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-heapq/},
note = {Permanent ID iil:note:zwsx3mfn. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-heapq.table.json (table; CC-BY-4.0)
- python-heapq.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-heapq.md.