🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Pop and return the largest item from the max-heap heap and also push the new item. The max-heap size doesn’t change. If the max-heap is empty, IndexError is raised. The value returned may be smaller than the item added.
🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
After popping, next smallest element (10) takes the root position. heapq.heappushpop() pushes a new element onto the heap and removes the smallest element in a single operation.
Published   April 6, 2026
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappop-method
Python heapq.heappop() Method - GeeksforGeeks
June 11, 2026 - Explanation: heapq.heappop(pq) removes the tuple with the smallest priority value.
🌐
Real Python
realpython.com › python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues – Real Python
July 18, 2022 - Since the root of the tree is the ... element. To pop the smallest element while preserving the heap property, the Python heapq module defines heappop()....
🌐
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 print 'random :', data heapq.heapify(data) print 'heapified :' show_tree(data) print inorder = [] while data: smallest = heapq.heappop(data) print 'pop =:' % smallest show_tree(data) inorder.append(smallest) print 'inorder :', inorder · $ python heapq_heappop.py random : [19, 9, 4, 10, 11, 8, 2] heapified : 2 9 4 10 11 8 19 ------------------------------------ pop 2: 4 9 8 10 11 19 ------------------------------------ pop 4: 8 9 19 10 11 ------------------------------------ pop 8: 9 10 19 11 ------------------------------------ pop 9: 10 11 19 ------------------------------------ pop 10: 11 19 ------------------------------------ pop 11: 19 ------------------------------------ pop 19: ------------------------------------ inorder : [2, 4, 8, 9, 10, 11, 19]
🌐
pocketpy
pocketpy.dev › modules › heapq
heapq | Portable Python 3.x Interpreter in Modern C - pocketpy
Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised.
🌐
APXML
apxml.com › courses › data-structures-algorithms-ml › chapter-5-heaps-priority-queues-ml › python-heapq
Python heapq Module for Heap Operations
When you pop, negate the result back. Use Tuples: Store tuples where the first element is the negated priority (or priority multiplied by -1) and the second element is the actual item. Python's tuple comparison compares elements lexicographically, so it will prioritize based on the first element (the negated priority). # Method 1: Negating values for a max-heap max_heap_negated = [] data = [10, 50, 20, 80, 30] for item in data: heapq.heappush(max_heap_negated, -item) # Push negative value print(f"Internal min-heap (negated): {max_heap_negated}") largest_item = -heapq.heappop(max_heap_negated)
🌐
Educative
educative.io › answers › what-is-the-heapqheappushpop-method-in-python
What is the heapq.heappushpop() method in Python?
The heappushpop method inserts a given item to the heap and then pops the smallest element from the heap. This method is equivalent to heappush() followed by heappop(). ... Line 1: We import the heapq module.
🌐
W3Schools
w3schools.com › python › ref_module_heapq.asp
Python heapq Module
The heapq module provides heap (priority queue) algorithms on regular Python lists. Use it to push/pop the smallest item efficiently and to implement priority-based workflows.
Find elsewhere
🌐
Python
docs.python.org › 3.0 › library › heapq.html
heapq — Heap queue algorithm — Python v3.0.1 documentation
Pop and return the smallest item from the heap, and also push the new item. The heap size doesn’t change. If the heap is empty, IndexError is raised. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap.
🌐
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). The removed value is returned and the heap is updated automatically. ... Return Value: Returns the smallest element after performing the push-pop ...
🌐
Real Python
realpython.com › ref › stdlib › heapq
heapq | Python Standard Library – Real Python
Language: Python · >>> import ... of the heap queue algorithm · Allows maintaining a list as a heap · Supports finding and popping the smallest item ·...
🌐
GitHub
github.com › python › cpython › blob › main › Lib › heapq.py
cpython/Lib/heapq.py at main · python/cpython
item = heapreplace(heap, item) # pops and returns smallest item, and adds · # new item; the heap size is unchanged · · Our API differs from textbook heap algorithms as follows: · - We use 0-based indexing. This makes the relationship between the · index for a node and the indexes for its children slightly less · obvious, but is more suitable since Python uses 0-based indexing.
Author   python
🌐
Educative
educative.io › answers › what-is-the-heapqheappop-method-in-python
What is the heapq.heappop() method in Python?
Note: Refer to What is a Heap? and What is the Python priority queue? to understand more about heaps and priority queues. The heappop method pops and returns the smallest element of the given heap. This method removes the smallest element.
🌐
Medium
medium.com › @derekfan › python-priority-queue-heap-queue-ec0b4dacb8ec
[Python] Priority queue & Heap. Heap | by Derek Fan | Medium
November 25, 2019 - heap[0]— access the smallest element without popping it, which is always the root. (called a “min heap”) heapq.heappop(heap) — pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError ...
🌐
Medium
medium.com › @pratik.bariya_67823 › heap-queue-in-python-c91c0fbadaa9
Heap Queue in Python. A heap queue, often referred to simply… | by Pratik Bariya | Medium
October 7, 2025 - import heapq data = [15,20,25,30,5,10] heapq.heapify(data) # appending element heapq.heappush(data, 35) print("heap :", data) # pop the smallest element heapq.heappop(data) print("heap :", data) # output: heap : [5, 15, 10, 30, 20, 25, 35] heap : [10, 15, 25, 30, 20, 35]
🌐
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.
🌐
Coderz Column
coderzcolumn.com › tutorials › python › heapq-heap-queue-priority-queue-implementation-in-python
heapq - Heap Queue / Priority Queue Implementation in Python by Sunny Solanki
February 10, 2021 - It also recreates the heap after the root node is popped to make sure all nodes satisfy the heap property. We are building on code from the last example in this example. We are first adding 10 numbers on the heap and then retrieving numbers one by one from the heap using heappop(). This returns us sorted elements. import heapq import random random.seed(123) heap = [] for i in range(10): val = random.randint(1,10) + random.random() heapq.heappush(heap, val) print("Heap : {}\n".format(heap)) for i in range(10): popped_element = heapq.heappop(heap) print(popped_element)