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 ...
10:34
Mastering Python heapq Module | Priority Queues, Heaps & Min-Heap ...
24:08
Heaps & Priority Queues - Heapify, Heap Sort, Heapq Library - DSA ...
15:57
Heaps & Priority Queues in Python - YouTube
Heapq Module And Priority Queue | Binary Heap | Python ...
04:47
Data Structures in Python | Heapq Module - YouTube
07:34
Python Heapq - YouTube
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
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)
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).
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
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.
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) ...
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 ·
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: ...
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 ------------------------------------ If the data is already in memory, it is more efficient to use heapify() to rearrange the items of the list in place.