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
When the two heaps have the same size, the new median is the average of the tops of the two heaps; otherwise, the median is at the top of the larger heap: def running_median(iterable): "Yields the cumulative median of values seen so far." lo = [] # max-heap hi = [] # min-heap (same size as or one smaller than lo) for x in iterable: if len(lo) == len(hi): heappush_max(lo, heappushpop(hi, x)) yield lo[0] else: heappush(hi, heappushpop_max(lo, x)) yield (lo[0] + hi[0]) / 2
Discussions

What do I use for a max-heap implementation in Python?
Using the heapq module with value negation is an effective way to implement a max-heap in Python. This approach leverages the efficiency of the heapq module while providing the functionality of a max-heap. For more comprehensive tutorials and practical examples on Python and other programming ... More on designgurus.io
๐ŸŒ designgurus.io
1
10
August 1, 2024
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
Make max heap functions public in heapq - Ideas - Discussions on Python.org
The heapq module contains some private max-heap variants of its heap functions: _heapify_max, _heappop_max, _heapreplace_max. This exist to support the higher-level functions like merge(). Iโ€™d like the _max variants to be made public (remove the underscore prefix), and documented. More on discuss.python.org
๐ŸŒ discuss.python.org
5
June 30, 2022
Python: using heapq module to find n largest items
Is the back end for this mod based on the quickselect algorithm? Thanks for sharing btw! More on reddit.com
๐ŸŒ r/Python
2
0
December 26, 2020
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
By default, Python's heapq implements a min-heap. To create a max-heap simply invert the values (store negative numbers).
Published ย  3 weeks ago
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python_data_structure โ€บ python_heaps.htm
Python - Heaps
If each parent node is greater ... called a max heap. It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ max-heap-in-python
Max Heap in Python - GeeksforGeeks
July 12, 2025 - By default Min Heap is implemented by this class. But we multiply each value by -1 so that we can use it as MaxHeap. ... # Python3 program to demonstrate heapq (Max Heap) from heapq import heappop, heappush, heapify # Create an empty heap h ...
๐ŸŒ
AlgoTree
algotree.org โ€บ algorithms โ€บ heap data structure
Python : Max Heap / Min Heap Using HeapQ :: AlgoTree
# We override the __lt__ (less than) or __gt__ (greater than) # function to convert the heapq of the objects into a min-heap or a max-heap # Create a max-heap by overriding the __gt__ operator. # def __gt__ (self, arg_obj) : # return self.freq < arg_obj.freq # Get a max-heap by overriding the ...
Find elsewhere
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ understanding-and-using-trees-in-python โ€บ lessons โ€บ solving-real-world-problems-with-heaps-in-python
Solving Real-World Problems with Heaps in Python
Let's delve into the implementation specifics. We'll use Python's built-in module heapq, which allows us to create a standard min heap. By storing numbers as negatives, we can simulate a max heap. First, we initialize two empty lists, which will serve as our heaps.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Make max heap functions public in heapq - Ideas - Discussions on Python.org
June 30, 2022 - The heapq module contains some private max-heap variants of its heap functions: _heapify_max, _heappop_max, _heapreplace_max. This exist to support the higher-level functions like merge(). Iโ€™d like the _max variants to bโ€ฆ
๐ŸŒ
Medium
medium.com โ€บ @mshoibkhan โ€บ heap-data-structure-in-python-min-head-and-max-heap-bd46218fcf8f
Heap Data Structure in Python | Min Head and Max Heap | by Shoib Khan | Medium
November 1, 2023 - The bigger value with negative values will become the smallest value that you can pop as absolute and your max heap is ready. ... from heapq import heappop, heappush def max_heap(li): h = [] # An empty list for heap insert for i in li: # Insert the value as negative heappush(h, -i) #pop the values as absolute and return return [abs( heappop(h) ) for _ in range(len(h))] #Call the function if __name__ == "__main__": hq = [5,7, 9, 8, 3, 2] data = max_heap(hq) print(data) #Output [9, 8, 7, 5, 3, 2]
๐ŸŒ
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
3. Extract: Extracting the maximum ... data structures. It assists in preserving the heap property within the heap. In Python, this operation can be executed using the heapify() function....
๐ŸŒ
Real Python
realpython.com โ€บ python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues โ€“ Real Python
July 18, 2022 - In this step-by-step tutorial, you'll explore the heap and priority queue data structures. You'll learn what kinds of problems heaps and priority queues are useful for and how you can use the Python heapq module to solve them.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-heapq-heappop-method
Python heapq.heappop() Method - GeeksforGeeks
July 23, 2025 - Since heapq only supports min-heaps, we can simulate a max-heap by negating the values when pushing and popping elements.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ guide-to-heaps-in-python
Guide to Heaps in Python
April 18, 2024 - For a max heap, as you ascend from ... in the maximum value at the pinnacle. In contrast, a min heap starts with the minimum value at its peak, with numbers escalating as you move downwards. As we progress, we'll dive deeper into how these inherent properties of heaps enable efficient operations ...
๐ŸŒ
Developer-service
developer-service.blog โ€บ understanding-pythons-heapq-module
Understanding Python's heapq Module
September 19, 2024 - In a max-heap, the value of I is greater than or equal to the values of its children, making the largest element the root. In Python, heapq implements a min-heap, meaning the smallest element is always at the root of the heap.
๐ŸŒ
Developer Indian
developerindian.com โ€บ articles โ€บ heap-and-priority-queue-in-python-a-complete-guide-with-examples
Heap and Priority Queue in Python: A Complete Guide with Examples
August 16, 2025 - Use PriorityQueue when working with multi-threaded programs. Always store priority as the first element of a tuple for proper ordering. For max-heap, store negative values since Pythonโ€™s heapq is min-heap by default.
๐ŸŒ
DEV Community
dev.to โ€บ devasservice โ€บ understanding-pythons-heapq-module-1n37
Understanding Python's heapq Module - DEV Community
September 19, 2024 - In a max-heap, the value of I is greater than or equal to the values of its children, making the largest element the root. In Python, heapq implements a min-heap, meaning the smallest element is always at the root of the heap.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_module_heapq.asp
Python heapq Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import heapq h = [] heapq.heappush(h, 3) heapq.heappush(h, 1) heapq.heappush(h, 2) print([heapq.heappop(h) for _ in range(3)]) Try it Yourself ยป