🌐
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 - Example: The following example inserts elements into a heap using heappush(). ... Explanation: heapq.heappush() inserts each element into h and automatically rearranges the heap so that the smallest element remains at index 0.
🌐
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 - This process is optimized due to the logarithmic nature of the binary heap structure, resulting in the overall time complexity of O(log n) for both heappush and heappop operations. Let’s demonstrate the time complexities with Python examples. import heapq # Creating a simple heap heap = [] heapq.heappush(heap, 4) heapq.heappush(heap, 1) heapq.heappush(heap, 7) heapq.heappush(heap, 3) print("Heap after push operations:", heap)
🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
heapq.heappush(heap, item) adds a new element to the heap. heapq.heappop(heap) removes and returns the smallest element. Example: This code demonstrates how to create a heap, append an element and remove the smallest element. Python ·
Published   April 6, 2026
🌐
W3Schools
w3schools.com › python › ref_module_heapq.asp
Python heapq Module
Python Examples Python Compiler ... Q&A Python Bootcamp Python Training · ❮ Standard Library Modules · Maintain a min-heap and pop the smallest items: import heapq h = [] heapq.heappush(h, 3) heapq.heappush(h, 1) heapq.heappush(h, ...
🌐
Educative
educative.io › answers › what-is-heapqheappush-in-python
What is heapq.heappush() in Python?
The heapq module is an inbuilt module in python. The module offers APIs for different operations of the heap data structure. Also, it provides min heap implementation where the parent key is less than or equal to those of its children.
🌐
APXML
apxml.com › courses › data-structures-algorithms-ml › chapter-5-heaps-priority-queues-ml › python-heapq
Python heapq Module for Heap Operations
You then operate on a standard Python list. To add an element to the heap while maintaining the heap property, use heapq.heappush(heap, item).
🌐
Medium
cleverzone.medium.com › exploring-pythons-heapq-module-b0c9d131545c
Exploring Python's heapq Module - Abhijeet Kumar
June 17, 2024 - heapq.heappush(heap, item) — Function in Python adds an item to an existing heap while maintaining the heap property.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › heapq.py
cpython/Lib/heapq.py at main · python/cpython
from _heapq import * except ImportError: pass · · # For backwards compatibility · _heappop_max = heappop_max · _heapreplace_max = heapreplace_max · _heappush_max = heappush_max · _heappushpop_max = heappushpop_max · _heapify_max = heapify_max ·
Author   python
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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) ...
🌐
DEV Community
dev.to › devasservice › understanding-pythons-heapq-module-1n37
Understanding Python's heapq Module - DEV Community
September 19, 2024 - The heapq module provides functions to perform heap operations on a regular Python list. ... To create a heap, you start with an empty list and use the heapq.heappush() function to add elements:
🌐
FavTutor
favtutor.com › blogs › heapq-python
Python's heapq module: Implementing heap queue algorithm
May 4, 2023 - The following table lists the time complexity of some of heapq's most popular functions. Depending on the heap size, heapify can take up to O(n) time to complete. heappush is complex O(log n), where n is the number of elements in the heap.
🌐
Real Python
realpython.com › ref › stdlib › heapq
heapq | Python Standard Library – Real Python
Language: Python · >>> import heapq >>> nums = [5, 1, 3, 7, 8, 2] >>> heapq.heapify(nums) >>> heapq.heappush(nums, 4) >>> nums [1, 5, 2, 7, 8, 3, 4] Popping the smallest item off the heap: Language: Python · >>> import heapq >>> nums = [5, 1, 3, 7, 8, 2] >>> heapq.heapify(nums) >>> heapq.heappop(nums) 1 ·
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_heaps.htm
Python - Heaps
In the resulting heap the smallest element gets pushed to the index position 0. But rest of the data elements are not necessarily sorted. heappush − This function adds an element to the heap without altering the current heap.
🌐
Python
docs.python.org › 3.0 › library › heapq.html
heapq — Heap queue algorithm — Python v3.0.1 documentation
This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement: ... >>> from heapq import heappush, heappop >>> heap = [] >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] >>> for item in data: ...
🌐
TechBeamers
techbeamers.com › python-heapq
The heapq (Heap Queue) Module in Python - TechBeamers
November 30, 2025 - Python’s heapq module provides a min-heap implementation using a binary heap structure. It offers seven key functions to work with priority queues, split into two categories: Four of them are primarily used for basic heap operations: heappush, heappop, heapify, and heapreplace.