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. For max-heaps, the following functions are provided:
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
Create new package similar to `heapq` but be able to pass custom comparator through a constructor - Ideas - Discussions on Python.org
The current heap container is okay in Python. It works as intended but you cannot pass a custom comparator and working with it feels “C like”, since you need to pass your list object each time. I’d be willing to make a new package so that we can have a similar heap/priority queue to java ... More on discuss.python.org
🌐 discuss.python.org
1
November 23, 2024
What do I use for a max-heap implementation in Python?
In Python, you can use the heapq module from the standard library to work with heaps. However, the heapq module provides a min-heap implementation by default. To implement a max-heap, you can adapt the heapq module to invert the values for comparison. More on designgurus.io
🌐 designgurus.io
1
10
August 1, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › max-heap-in-python
Max Heap in Python - GeeksforGeeks
July 12, 2025 - """ Python3 program to implement MaxHeap using heapq for Strings, Numbers, and Objects """ from functools import total_ordering import heapq @total_ordering class Wrap: def __init__(self, v): self.v = v def __lt__(self, o): return self.v > o.v # Reverse for Max Heap def __eq__(self, o): return self.v == o.v # Max Heap for numbers h = [10, 20, 400, 30] wh = list(map(Wrap, h)) heapq.heapify(wh) print("Max:", heapq.heappop(wh).v) # Max Heap for strings h = ["this", "code", "is", "wonderful"] wh = list(map(Wrap, h)) heapq.heapify(wh) print("Heap:", end=" ") while wh: print(heapq.heappop(wh).v, end=" ")
🌐
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…
Find elsewhere
🌐
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.
🌐
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]
🌐
Python.org
discuss.python.org › ideas
Make max heap functions public in heapq - #12 by EklipZgit - Ideas - Discussions on Python.org
March 28, 2024 - 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…
🌐
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   2 weeks ago
🌐
Python Cheat Sheet
pythonsheets.com › notes › basic › python-heap.html
Heap — Python Cheat Sheet
Python’s heapq only provides a min-heap. To implement a max-heap, negate the values when pushing and negate again when popping.
🌐
Python.org
discuss.python.org › ideas
Create new package similar to `heapq` but be able to pass custom comparator through a constructor - Ideas - Discussions on Python.org
November 23, 2024 - The current heap container is okay in Python. It works as intended but you cannot pass a custom comparator and working with it feels “C like”, since you need to pass your list object each time. I’d be willing to make a new package so that we can have a similar heap/priority queue to java ...
🌐
Verve AI
vervecopilot.com › interview-questions › what-no-one-tells-you-about-max-heap-python-and-interview-performance
What No One Tells You About Max Heap Python And Interview Performance
Implementing a max heap python ... To simulate a max heap using heapq, you must negate the values before pushing them onto the heap and then negate them back when popping [^3]. For example, to add 5 to a max heap, you'd push ...
🌐
Reddit
reddit.com › r/python › question on heapq design - why no maxheap implementation?
r/Python on Reddit: Question on heapq design - why no maxheap implementation?
April 11, 2022 -

I am working through grokking the coding interview and decided to use python due to it's readability and overall simplicity in its syntax.

This morning I started working on the 'two heaps' algorithms. It struck me as a bit odd that python or the writers of the heapq library decided to make all implementations of heap minheaps rather than adding some additional APIs for maxheaps.

Maybe it's just me, but I find it a bit hard to reason through programs that make use of maxheaps. Having to remember to push a value multiplied by -1 and then do the same for retrieval feels a bit un-intuitive, but maybe it's just me.

Does anyone know of the reasoning behind not implementing them separately and adding a thin layer to the maxheaps to avoid having to do this? I'm mostly just curious if there was any discussion around it when heapq was created but haven't been able to find anything yet.

🌐
YouTube
youtube.com › watch
Heaps & Priority Queues - Heapify, Heap Sort, Heapq Library - DSA Course in Python Lecture 9 - YouTube
Code solutions in Python, Java, C++ and JS can be found at my GitHub repository here: https://github.com/gahogg/Data-Structures-and-Algorithms-Theory-Course-...
Published   July 16, 2024
🌐
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.
🌐
Medium
medium.com › @kevinchwong › python-code-gems-make-you-look-smarter-7-using-heaps-to-find-k-largest-smallest-items-5b1393ae0e97
Python code gems make you look smarter (7) — Using Heaps to Find K Largest/Smallest Items | by Kevin Wong | Medium
September 1, 2024 - In a max-heap, each node is larger than its children, and the root (11) is the largest element. Python’s heapq module provides an implementation of a min-heap ONLY.
🌐
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 this example, we define a function create_max_heap() that takes a list of elements as input. The elements are negated and then transformed into a heap in-place using heapq.heapify().