heapreplace(a, x) returns the smallest value originally in a regardless of the value of x, while, as the name suggests, heappushpop(a, x) pushes x onto a before popping the smallest value. Using your data, here's a sequence that shows the difference:

>>> from heapq import *
>>> a = [2,7,4,0,8,12,14,13,10,3,4]
>>> heapify(a)
>>> b = a[:]
>>> heappushpop(a, -1)
-1
>>> heapreplace(b, -1)
0
Answer from Tim Peters on Stack Overflow
🌐
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 › python-heapq-heappop-method
Python heapq.heappop() Method - GeeksforGeeks
July 23, 2025 - The heapq.heappop() function in Python is used to pop and return the smallest element from a heap, maintaining the heap property.
Discussions

heap - python, heapq: difference between heappushpop() and heapreplace() - Stack Overflow
If you just use pop + push (like in java), it will be two times slower :( ... @spspli because bubble down needs to take at most h steps, where h is the height of the heap. With every step it goes down one level in that tree. And h is log(K). 2020-06-25T15:35:24.51Z+00:00 ... Although the chosen answer answers the question, this is much more infromative and useful. Thanks! 2023-10-20T02:10:24.21Z+00:00 ... >>> seq [0, 1, 5, 2, 6, 7, 9, 3] >>> heapq... More on stackoverflow.com
🌐 stackoverflow.com
python - How to efficiently pop all elements with the smallest key in heapq? - Stack Overflow
I am working on a simulation experiment and trying to make my code as efficient as possible. In one part, I have a min heap priority queue that I have implemented using heapq module. Throughout the simulation, I have to pop all elements with the smallest key. More on stackoverflow.com
🌐 stackoverflow.com
How to use Python's heapq as min-heap AND max-heap?
Just add negative values. And file fetching add - to it. More on reddit.com
🌐 r/leetcode
10
9
April 8, 2024
python - How to make heapq evaluate the heap off of a specific attribute? - Stack Overflow
I wish to hold a heap of objects, not just numbers. They will have an integer attribute in them that the heap can sort by. The easiest way to use heaps in python is heapq, but how do I tell it to... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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   2 weeks ago
🌐
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()....
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
Real Python
realpython.com › ref › stdlib › heapq
heapq | Python Standard Library – Real Python
Offers functions to push and pop items while maintaining heap order · Transforming a list into a heap: Python · >>> import heapq >>> nums = [5, 1, 3, 7, 8, 2] >>> heapq.heapify(nums) >>> nums [1, 5, 2, 7, 8, 3] Pushing a new item onto the heap: Python · >>> heapq.heappush(nums, 4) >>> nums [1, 4, 2, 7, 8, 3, 5] Popping the smallest item off the heap: Python ·
🌐
Medium
cleverzone.medium.com › exploring-pythons-heapq-module-b0c9d131545c
Exploring Python's heapq Module - Cleverzone
June 17, 2024 - First heappop — heapq.heappop(numbers) pops the smallest element (1) from the heap.
🌐
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]
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappushpop-method
Python heapq.heappushpop() Method - GeeksforGeeks
July 23, 2025 - This method is a combination of two operations: heappush() and heappop(). It allows you to push a new element onto the heap and then pop the smallest element in one atomic operation, ensuring efficiency in heap-based algorithms.
🌐
DEV Community
dev.to › devasservice › understanding-pythons-heapq-module-1n37
Understanding Python's heapq Module - DEV Community
September 19, 2024 - The task with the lowest priority value is always popped first. The heapq module in Python is a powerful tool for efficiently managing data that needs to maintain a sorted order based on priority.