🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappush-method
Python heapq.heappush() Method - GeeksforGeeks
June 11, 2026 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 11 Jun, 2026 · heapq.heappush() function inserts an element into a heap while maintaining the heap property.
🌐
Medium
dpythoncodenemesis.medium.com › understanding-pythons-heapq-module-a-guide-to-heap-queues-cfded4e7dfca
Understanding Python’s Heapq Module: A Guide to Heap Queues | by Python Code Nemesis | Medium
October 21, 2023 - Heapq is a module in Python that provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. It allows for efficient management of priority queues in a way that elements with higher priority are served ...
🌐
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.
Find elsewhere
🌐
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
🌐
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
🌐
Real Python
realpython.com › python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues – Real Python
July 18, 2022 - The Python heapq module also includes heappush() for pushing an element to the heap while preserving the heap property.
🌐
Medium
cleverzone.medium.com › exploring-pythons-heapq-module-b0c9d131545c
Exploring Python's heapq Module - Cleverzone
June 17, 2024 - heapq.heappush(heap, item) — Function in Python adds an item to an existing heap while maintaining the heap property.
🌐
FavTutor
favtutor.com › blogs › heapq-python
Python's heapq module: Implementing heap queue algorithm
May 4, 2023 - import heapq heap = [] heapq.heappush(heap, -3) heapq.heappush(heap, -8) heapq.heappush(heap, -1) print([-x for x in heap]) ... Sorting the data is a common application of the built-in heapq module and sorted function in Python.
🌐
TechBeamers
techbeamers.com › python-heapq
The heapq (Heap Queue) Module in Python - TechBeamers
November 30, 2025 - Elements in the heap: ('H', 1) ('H', 4) ('H', 7) ('H', 9) ---------------------- Calling heappushpop() to push element on the heap and return the smallest one. ('H', 4) ('H', 9) ('H', 7) ('H', 11) Write a Python program to perform Heap Sort, ...
🌐
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).
🌐
Runebook.dev
runebook.dev › en › docs › python › library › heapq › heapq.heappush
python - Heapq.heappush() Explained: Common Pitfalls and Max-Heap Secrets
It uses a standard Python list to represent a min-heap, where the smallest element is always at the root (index 0). The heapq.heappush(heap, item) function pushes the value item onto the heap, maintaining the heap invariant.
🌐
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