Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Source code: Lib/heapq.py This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. Min-heaps are binary trees for which every parent node has ...
W3Schools
w3schools.com › python › ref_module_heapq.asp
Python heapq Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import heapq h = [] heapq.heappush(h, 3) heapq.heappush(h, 1) heapq.heappush(h, 2) print([heapq.heappop(h) for _ in range(3)]) Try it Yourself »
GeeksforGeeks
geeksforgeeks.org › python › heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
Explanation: heappushpop(h, 5) first pushes 5 into the heap and immediately pops the smallest element (which is also 5).
Published April 6, 2026
CodeSignal
codesignal.com › learn › courses › understanding-and-using-trees-in-python › lessons › unraveling-heaps-theory-operations-and-implementations-in-python
Theory, Operations, and Implementations in Python
Python offers a vast range of libraries, including a built-in module, heapq, which allows for the creation and manipulation of heaps with ease. import heapq heap = [] # Insert in heap heapq.heappush(heap, 4) heapq.heappush(heap, 9) heapq.heappush(heap, 6) print("Heap after insertion: ", heap) # Output: Heap after insertion: [4, 9, 6] # Delete the smallest element from the heap heapq.heappop(heap) print("Heap after deletion: ", heap) # Output: Heap after deletion: [6, 9] # Extract the smallest element smallest = heapq.nsmallest(1, heap)[0] print("Smallest element in the heap: ", smallest) # Output: Smallest element in the heap: 6 ·
Python Module of the Week
pymotw.com › 2 › heapq
heapq – In-place heap sort algorithm - Python Module of the Week
import heapq from heapq_showtree import show_tree from heapq_heapdata import data heap = [] print 'random :', data print for n in data: print 'add =:' % n heapq.heappush(heap, n) show_tree(heap) $ python heapq_heappush.py random : [19, 9, 4, 10, 11, 8, 2] add 19: 19 ------------------------------------ add 9: 9 19 ------------------------------------ add 4: 4 19 9 ------------------------------------ add 10: 4 10 9 19 ------------------------------------ add 11: 4 10 9 19 11 ------------------------------------ add 8: 4 10 8 19 11 9 ------------------------------------ add 2: 2 10 4 19 11 9 8 ------------------------------------
Pythontic
pythontic.com › algorithms › heapq › heappush
heappush function of heapq module in Python | Pythontic.com
The heappush() function from the heapq module of Python adds an element into an existing heap while maintaining the heap property. The Python example creates a heap, adds elements to it and prints the heap.
Educative
educative.io › answers › what-is-heapqheappush-in-python
What is heapq.heappush() in Python?
The heappush method inserts the given item onto the heap.
Python Cheat Sheet
pythonsheets.com › notes › basic › python-heap.html
Heap — Python Cheat Sheet
>>> import heapq >>> # Convert list to heap in-place >>> h = [5, 1, 3, 2, 6] >>> heapq.heapify(h) >>> h[0] # smallest element at root 1 >>> # Push and pop >>> heapq.heappush(h, 0) >>> heapq.heappop(h) 0 >>> # Push and pop in one operation >>> heapq.heappushpop(h, 4) # push 4, then pop smallest 1 >>> # Pop and push in one operation >>> heapq.heapreplace(h, 0) # pop smallest, then push 0 2
Top answer 1 of 9
166
According to the example from the documentation, you can use tuples, and it will sort by the first element of the tuple:
>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')
So if you don't want to (or can't?) do a __cmp__ method, you can manually extract your sorting key at push time.
Note that if the first elements in a pair of tuples are equal, further elements will be compared. If this is not what you want, you need to ensure that each first element is unique.
2 of 9
114
heapq sorts objects the same way list.sort does, so just define a method __cmp__() within your class definition, which will compare itself to another instance of the same class:
def __cmp__(self, other):
return cmp(self.intAttribute, other.intAttribute)
Works in Python 2.x.
In 3.x use:
def __lt__(self, other):
return self.intAttribute < other.intAttribute
APXML
apxml.com › courses › data-structures-algorithms-ml › chapter-5-heaps-priority-queues-ml › python-heapq
Python heapq Module for Heap Operations
# Example using heappushpop (maintaining top 3 elements) top_k_heap = [10, 20, 30] # Assume this is already a heap heapq.heapify(top_k_heap) # Ensure it's a heap # Process new items, keeping only the top 3 largest (using min-heap for smallest) # If we model this as keeping the 'k' largest using a min-heap of size 'k', # we push a new item and pop the smallest if the heap size exceeds 'k'. # heappushpop is useful if the heap is already at size 'k'. new_item = 5 if len(top_k_heap) < 3: heapq.heappush(top_k_heap, new_item) elif new_item > top_k_heap[0]: # Only proceed if larger than the smallest
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappushpop-method
Python heapq.heappushpop() Method - GeeksforGeeks
June 15, 2026 - Explanation: heapq.heappushpop(h, 5) inserts 5 into the heap and removes the smallest element (2).
Reddit
reddit.com › r/leetcode › complexity analysis of heappush, heappop and heapify in python.
r/leetcode on Reddit: Complexity analysis of heappush, heappop and heapify in Python.
January 5, 2023 -
What is the time complexity for these?
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › heapq.heappush.html
heapq.heappush() — Python Standard Library
Docs » · api » · heapq » · heapq.heappush() · View page source · heapq · heappush · (heap, item) → None. Push item onto heap, maintaining the heap invariant.¶ · Next Previous