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 ... 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) ...
Discussions

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
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
What would happen if I use max-heap instead of min-heap for priority queue in Dijkstra's algorithm? Will it work?
Serious answer: if you were forced to use a max-heap to build it for whatever reason, you can. Just flip the sign on the distance metric! Most minimization problems can be transformed to maximization problems and vice-versa, although one framing might be more useful or intuitive than the other. More on reddit.com
๐ŸŒ r/compsci
12
8
September 16, 2024
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ max-heap-in-python
Max Heap in Python - GeeksforGeeks
July 12, 2025 - The maxHeap is PARENT : 84 LEFT CHILD : 22 RIGHT CHILD : 19 PARENT : 22 LEFT CHILD : 17 RIGHT CHILD : 10 PARENT : 19 LEFT CHILD : 5 RIGHT CHILD : 6 PARENT : 17 LEFT CHILD : 3 RIGHT CHILD : 9 The Max val is 84 ยท We use heapq class to implement Heap in Python.
๐ŸŒ
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). Example: Below example, convert a list into a max-heap by storing negative numbers and then retrieve the largest element:
Published ย  2 weeks ago
๐ŸŒ
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 - ... 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))] ...
๐ŸŒ
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 (for Max Heap) or minimum (for Min Heap) is a constant-time operation, as the maximum or the minimum element is always at the root of the heap. ... The "Heapify" method is an intriguing function used to rearrange elements in heap data structures. It assists in preserving the heap property within the heap. In Python, this operation can be executed using the heapify() function.
Find elsewhere
๐ŸŒ
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โ€ฆ
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python_data_structure โ€บ python_heaps.htm
Python - Heaps
But you can apply heapify function again to bring the newly added element to the first index only if it smallest in value. In the below example we insert the number 8. import heapq H = [21,1,45,78,3,5] # Covert to a heap heapq.heapify(H) print(H) ...
๐ŸŒ
AlgoTree
algotree.org โ€บ algorithms โ€บ heap data structure
Python : Max Heap / Min Heap Using HeapQ :: AlgoTree
def __lt__ (self, arg_obj) : return ... = Mountain("Kangchenjunga", 8586) m3 = Mountain("Everest", 8848) m4 = Mountain("Annapurna", 8091) max_heap_mountains = [m1, m2, m3, m4] heapq.heapify(max_heap_mountains) print("Max heap using heapq") print("Arranging mo...
๐ŸŒ
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.
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ python โ€บ max heap implementation in python using heapq
Max heap implementation in Python using heapq | Techie Delight
September 12, 2025 - The following program provides a simple implementation of max heap for integers using heapq operations. It can be easily extended to support any other general-purpose functions based on heaps. ... For objects, we can directly use the heapq module in Python to get a max heap.
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ heapq
heapq โ€“ In-place heap sort algorithm - Python Module of the Week
$ 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 ------------------------------------ ...
๐ŸŒ
DEV Community
dev.to โ€บ devasservice โ€บ understanding-pythons-heapq-module-1n37
Understanding Python's heapq Module - DEV Community
September 19, 2024 - This guide will explain the basics of heaps and how to use the heapq module and provide some practical examples. A heap is a special tree-based data structure that satisfies the heap property: In a min-heap, for any given node I, the value of I is less than or equal to the values of its children. Thus, the smallest element is always at the root. 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.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ guide-to-heaps-in-python
Guide to Heaps in Python
April 18, 2024 - This means that the smallest element is always at the root (or the first position in the list). If you need a max heap, you'd have to invert order by multiplying elements by -1 or use a custom comparison function. Python's heapq module provides a suite of functions that allow developers to ...
๐ŸŒ
Developer-service
developer-service.blog โ€บ understanding-pythons-heapq-module
Understanding Python's heapq Module
September 19, 2024 - import heapq heap = [] heapq.heappush(heap, 10) heapq.heappush(heap, 5) heapq.heappush(heap, 20) After these operations, heap will be [5, 10, 20], with the smallest element at index 0. The smallest element can be accessed without removing it ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to implement a max heap in python
5 Best Ways to Implement a Max Heap in Python - Be on the Right Side of Change
March 10, 2024 - In the MaxHeap class shown above, we use heapq.heappush() and heapq.heappop() on a list, but each value is negated before insertion and negated again before retrieval. This ensures a max heap property without deviating from the built-in heapq functionality. If weโ€™re dealing with objects in a heap, we can implement a max heap by providing a custom sorting key that takes into account the property we wish to prioritize.