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 OverflowGeeksforGeeks
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=" ")
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
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
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
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
Videos
07:26
Python: MaxHeap heapsort - YouTube
24:08
Heaps & Priority Queues - Heapify, Heap Sort, Heapq Library - DSA ...
05:32
MAXIMAL SCORE AFTER APPLYING K OPERATIONS | LEETCODE 25230 | PYTHON ...
15:57
Heaps & Priority Queues in Python - YouTube
37:30
Heap - Data Structures in Python #6 - YouTube
01:59
Max-Heap Implementation in Python - YouTube
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
Top answer 1 of 16
527
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.
2 of 16
426
You can use
import heapq
listForTree = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
heapq.heapify(listForTree) # for a min heap
heapq._heapify_max(listForTree) # for a maxheap!!
If you then want to pop elements, use:
heapq.heappop(minheap) # pop from minheap
heapq._heappop_max(maxheap) # pop from maxheap
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.
LinkedIn
linkedin.com › posts › navdeep-singh-3aaa14161_python-finally-supports-native-max-heap-operations-activity-7393793665313226752-tFr2
Python 3.14 adds native max heap operations to heapq ...
We cannot provide a description for this page right now
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
Reddit
reddit.com › r/leetcode › how to use python's heapq as min-heap and max-heap?
r/leetcode on Reddit: How to use Python's heapq as min-heap AND max-heap?
April 8, 2024 -
I know that heapq in python is min-heap (first element is the smallest).
How do I initialize a max-heap, where the root is the largest?
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.
Naukri
naukri.com › code360 › library › max-heap-in-python
Max Heap in Python - Naukri Code 360
August 21, 2025 - Almost there... just a few more seconds
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).
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.