The easiest way is to invert the value of the keys and use heapq. For example, turn 1000.0 into -1000.0 and 5.0 into -5.0.

Answer from Daniel Stutzbach on Stack Overflow
🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
The value returned may be larger than the item added. If that isn’t desired, consider using heappushpop() instead. Its push/pop combination returns the smaller of the two values, leaving the larger value on the heap.
🌐
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

data structures - What do I use for a max-heap implementation in Python? - Stack Overflow
Python includes the heapq module for min-heaps, but I need a max-heap. What should I use for a max-heap implementation in Python? More on stackoverflow.com
🌐 stackoverflow.com
python - What is the difference between heappop and pop(0) on a sorted list? - Stack Overflow
EDIT: I know heappop() pops the smallest value out, which is why I have sorted the list based off of the 'a' (which heappop uses too, I assume) ... You do not need to sort the list again and again. You can move the list_.sort() call before the loop. What problem does it cause in other part of your code? ... To work with heapq you have to be aware python ... More on stackoverflow.com
🌐 stackoverflow.com
Complexity analysis of heappush, heappop and heapify in Python.
Heappush and Heappop -O(log(n)) Heapify - O(n) because it uses sift down operations Space complexity is O(n) More on reddit.com
🌐 r/leetcode
5
8
January 5, 2023
How does heap pop works when popping tuple items?
Yes, that's correct. And it's not just heaps; python compares all collections of things that way in all contexts. >>> sorted([(1, 99), (1, 10)]) [(1, 10), (1, 99)] >>> sorted(["ab", "aa"]) ['aa', 'ab'] More on reddit.com
🌐 r/learnpython
5
1
August 3, 2023
🌐
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   3 weeks ago
🌐
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.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 66130557 › what-is-the-difference-between-heappop-and-pop0-on-a-sorted-list
python - What is the difference between heappop and pop(0) on a sorted list? - Stack Overflow
heap.heappush(list_, (a, b)) # b is a tuple while len(list_): temp = heap.heappop(list_)[1] ... list_.append(a, b) # b is a tuple while len(list_): list_.sort(key = lambda x: x[0]) temp = list_.pop(0)[1]
🌐
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
🌐
Reddit
reddit.com › r/learnpython › how does heap pop works when popping tuple items?
r/learnpython on Reddit: How does heap pop works when popping tuple items?
August 3, 2023 -

I've saw that when setting tuple item as heap element, it would get the first value while popping minimum values from heap.

heap = [(1, 10), (2, 99)]

For the above heap, it's obvious that it would pop the element (1, 10) first, since first elemnt of tuple 1 < 2

However, for those tuple elements where first element is same

heap = [(1, 10), (1, 99)]

I've tested some cases, heap would drop the element (1, 10) first, looks like it compares the second element when first element is same for multiples.

Is that the correct logic that heap would compare following values while first element is the same for tuples?

🌐
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
🌐
Python.org
discuss.python.org › ideas
Searching and removing events from heapq (heap queue) based on marker/identifier - Ideas - Discussions on Python.org
February 13, 2024 - Background The Python heapq module provides an efficient implementation of the heap queue algorithm, also known as the priority queue algorithm. It allows for the addition of elements in a way that the smallest (or largest, with a minor ...
🌐
Medium
cleverzone.medium.com › exploring-pythons-heapq-module-b0c9d131545c
Exploring Python’s heapq Module
June 17, 2024 - First heappop — heapq.heappop(numbers) pops the smallest element (1) from the heap.
🌐
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.
🌐
Real Python
realpython.com › python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues – Real Python
July 18, 2022 - Once the node is added, Python compares it to its parent. If the heap property is violated, then the node and its parent are switched, and the check begins again at the parent. This continues until the heap property holds or the root has been reached. Similarly, when popping the smallest element, Python knows that, because of the heap property, the element is at the root of the tree.
🌐
Python Module of the Week
pymotw.com › 2 › heapq
heapq – In-place heap sort algorithm - Python Module of the Week
Replacing elements in place lets you maintain a fixed size heap, such as a queue of jobs ordered by priority. $ python heapq_heapreplace.py start: 2 9 4 10 11 8 19 ------------------------------------ replace 2 with 0: 0 9 4 10 11 8 19 ------------------------------------ replace 0 with 7: 4 9 7 10 11 8 19 ------------------------------------ replace 4 with 13: 7 9 8 10 11 13 19 ------------------------------------ replace 7 with 9: 8 9 9 10 11 13 19 ------------------------------------ replace 8 with 5: 5 9 9 10 11 13 19 ------------------------------------
🌐
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.
🌐
YouTube
youtube.com › chris tralie
Heap Tutorial Part 2: Push And Pop Implementations - YouTube
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new featuresNFL Sunday Ticket · © 2024 Google LLC
Published   November 26, 2024
Views   5
🌐
Developer-service
developer-service.blog › understanding-pythons-heapq-module
Understanding Python's heapq Module
September 19, 2024 - The heapq module in Python provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.