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
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › python-program-for-heap-sort
Heap Sort - Python - GeeksforGeeks
January 16, 2026 - Ensures every parent node is greater than its children, making the largest element the root of the heap. The loop for i in range(n - 1, 0, -1) extracts the maximum element (root) one by one.
🌐
GeeksforGeeks
geeksforgeeks.org › python › max-heap-in-python
Max Heap in Python - GeeksforGeeks
July 12, 2025 - Override the __lt__ dunder method to give inverse result. Following is the implementation of the method mentioned here. ... """ 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=" ")
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
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
Question on heapq design - why no maxheap implementation?
I wondered about that as well. Now I'm just used to doing negative multiplication for max heap. More on reddit.com
🌐 r/Python
11
3
April 11, 2022
🌐
GitHub
github.com › notini › python_max_heap
GitHub - notini/python_max_heap: Python implementation of a Max Heap based on Cormen's 'Introduction to Algorithms' book for educational purposes. · GitHub
values = [4,1,3,2,16,9,10,14,8,7] for idx in range(math.floor(len(values) / 2), 0, -1): max_heap.max_heapify(values, len(values), idx - 1)
Author   notini
🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Added in version 3.14. ... Pop and return the largest item from the max-heap heap, maintaining the max-heap invariant. If the max-heap is empty, IndexError is raised.
🌐
Board Infinity
boardinfinity.com › blog › heap-in-python
Heap Queue (or Heapq) in Python | Board Infinity
June 22, 2023 - Heap is a data structure, that is mainly used to represent a priority queue. In Python, it is available by importing the heapq module. Heapq has a property that every time the smallest heap element is popped (min-heap). Each time when an element is pushed or popped the heap structure is maintained.
Find elsewhere
🌐
Hello Interview
hellointerview.com › learn › code
Data Structures and Algorithms Introduction | Hello Interview
This guide uses interactive visualizations to teach you the most important algorithm patterns for the coding interview.
🌐
Medium
medium.com › @allan.sioson › max-heapify-build-max-heap-and-heapsort-algorithm-in-python-42c4dec70829
Max-Heapify, Build-Max-Heap, and Heapsort Algorithm | by Allan A. Sioson | Medium
October 17, 2023 - Any given array A can be transformed to a max heap by repeatedly using the Max-Heapify algorithm. Let’s call this algorithm as the Build-Max-Heap algorithm. The implementation uses the Max-Heapify algorithm starting from the last node with at least one child up to the root node. An implementation in python is given below:
🌐
Wikipedia
en.wikipedia.org › wiki › Sorting_algorithm
Sorting algorithm - Wikipedia
1 week ago - Insertion sort is widely used for small data sets, while for large data sets an asymptotically efficient sort is used, primarily heapsort, merge sort, or quicksort. Efficient implementations generally use a hybrid algorithm, combining an asymptotically efficient algorithm for the overall sort with insertion sort for small lists at the bottom of a recursion. Highly tuned implementations use more sophisticated variants, such as Timsort (merge sort, insertion sort, and additional logic), used in Android, Java, and Python, and introsort (quicksort and heapsort), used (in variant forms) in some C++ sort implementations and in .NET.
🌐
Apache Kafka
kafka.apache.org › documentation
Documentation Redirect | Apache Kafka
February 16, 2026 - Redirecting · Security | Donate | Thanks | Events | License | Privacy
🌐
W3Schools
w3schools.com › dsa › dsa_theory_trees.php
W3Schools.com
Routing Tables: Used for routing data in network algorithms. Sorting/Searching: Used for sorting data and searching for data. Priority Queues: Priority queue data structures are commonly implemented using trees, such as binary heaps.
🌐
Educative
educative.io › answers › heap-implementation-in-python
Heap implementation in Python
From this definition, we can infer that we can use heaps to retrieve the maximum or minimum object in constant time.
🌐
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
In simpler terms, in a Max Heap, each parent node is greater than or equal to its child node(s), and in a Min Heap, each parent node is less than or equal to its child node(s).
🌐
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 - ... In the max heap, every root node has a value greater than or equal to its children, we use an array in which arr[ r ] ≥ arr[ 2*r + 1 ] and arr[ r ] ≥ arr[ 2*r + 2 ], where r is the index of the node value in the array.
🌐
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…
🌐
University of San Francisco
cs.usfca.edu › ~galles › visualization › Algorithms.html
Data Structure Visualization
Insertion Sort · Shell Sort · Merge Sort · Quck Sort · Bucket Sort · Counting Sort · Radix Sort · Heap Sort · Heap-like Data Structures · Heaps · Binomial Queues · Fibonacci Heaps · Leftist Heaps · Skew Heaps · Graph Algorithms · Breadth-First Search ·
🌐
FavTutor
favtutor.com › blogs › heapq-python
Python's heapq module: Implementing heap queue algorithm
May 4, 2023 - nlargest has a time complexity of O(n log k), where n is the total number of elements in the iterable and k is the maximum size desired for the returned set. nsmallest has an O(1) time complexity, where n is the total number of elements in the iterable and k is the minimum number of elements to return.(n log k). Clearly, the majority of heapq functions are quite efficient, with a time complexity of O(log n) or O(n log k). Therefore, heapq is a viable option when working with Python's heap data structure and larger datasets.