According to the example from the documentation, you can use tuples, and it will sort by the first element of the tuple:

>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')

So if you don't want to (or can't?) do a __cmp__ method, you can manually extract your sorting key at push time.

Note that if the first elements in a pair of tuples are equal, further elements will be compared. If this is not what you want, you need to ensure that each first element is unique.

Answer from Jander on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › python › max heap python
How to Get Max Heap in Python | Delft Stack
February 2, 2024 - The new list is then converted to a heap using heapify(). To pop the maximum value, we use heappop() on the heap, convert the tuple to a list, modify the first element to get a positive value, then convert the list back to a tuple.
🌐
Reddit
reddit.com › r/learnpython › how does heap pop works when popping tuple items?
r/learnpython on Reddit: How does heap pop works when popping tuple items?
August 3, 2023 -

I've saw that when setting tuple item as heap element, it would get the first value while popping minimum values from heap.

heap = [(1, 10), (2, 99)]

For the above heap, it's obvious that it would pop the element (1, 10) first, since first elemnt of tuple 1 < 2

However, for those tuple elements where first element is same

heap = [(1, 10), (1, 99)]

I've tested some cases, heap would drop the element (1, 10) first, looks like it compares the second element when first element is same for multiples.

Is that the correct logic that heap would compare following values while first element is the same for tuples?

🌐
Interviewcrunch
interviewcrunch.com › python › advanced-data-structures › heap
Heap | InterviewCrunch: Coding Interviews Broken Down
If the items in the heap are tuples, ... value in the heap: ... If you want to create a max heap (a heap that will return the maximum value), use a negative priority....
🌐
GeeksforGeeks
geeksforgeeks.org › python › max-heap-in-python
Max Heap in Python - GeeksforGeeks
July 12, 2025 - We use heapq class to implement Heaps in Python. By default Min Heap is implemented by this class. To implement MaxHeap not limiting to only numbers but any type of object(String, Tuple, Object etc) we should
🌐
SSOJet
ssojet.com › data-structures › implement-heap-in-python
Implement Heap in Python | Implement Data Structures in Programming Languages
Alternatively, and often simpler, you can store your objects within tuples where the first element is the attribute you want to sort by. Consider storing employee records where you need to prioritize by salary. To create a max-heap (highest salary first), you'd negate the salary value:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
import heapq nums = [10, 20, 15, 30, 40] # Convert into a max-heap by inverting values max_heap = [-n for n in nums] heapq.heapify(max_heap) # Access largest element (invert sign again) print("Largest element:", -max_heap[0])
Published   3 weeks ago
🌐
AlgoMap
algomap.io › lessons › heaps
Heaps & Priority Queues | AlgoMap
The heap compares elements by the first tuple item, making it ideal for implementing priority queues. If priorities are equal, it compares the next tuple element. Since heapq is a Min Heap by default, you can simulate a Max Heap by inserting the negated value.
🌐
TheLinuxCode
thelinuxcode.com › home › max heap in python: a practical, production-ready guide
Max Heap in Python: A Practical, Production-Ready Guide – TheLinuxCode
February 18, 2026 - For a max heap, reverse or wrap as needed. This gives deterministic ordering when priorities tie. If you can’t update an item in a heap easily, don’t mutate it. Push a new entry and mark the old one as stale. When you pop, discard stale entries until you find a valid one.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › heapq › heapq.heappush
python - Heapq.heappush() Explained: Common Pitfalls and Max-Heap Secrets
import heapq max_heap = [] values_to_add = [4, 1, 7, 3] print("--- Max-Heap Simulation ---") for value in values_to_add: # Push the negative of the value negated_value = -value heapq.heappush(max_heap, negated_value) print(f"Pushed: {value}, Heap state: {max_heap}") # The smallest *negative* value is -7, which corresponds to the largest positive 7 largest_item = -heapq.heappop(max_heap) # Pop the smallest negative and negate it back print(f"\nLargest item retrieved (Max-Heap pop): {largest_item}") # Output: Largest item retrieved (Max-Heap pop): 7 · If you try to push complex objects (like a custom class instance) that Python doesn't know how to compare, you'll get a TypeError. Troubleshooting / Solution When pushing custom objects, you need to use a tuple where the first element is the value you want to use for comparison (the priority).
🌐
GeeksforGeeks
geeksforgeeks.org › python › heapq-with-custom-predicate-in-python
Heapq with custom predicate in Python - GeeksforGeeks
July 23, 2025 - The heapq module has several functions that take the list as a parameter and arranges it in a min-heap order. The problem with these functions is they expect either a list or a list of tuples as a parameter. They do not support comparisons between any other iterable or objects.
🌐
Medium
medium.com › @ksaquib › mastering-arrays-and-strings-a-comprehensive-guide-in-python-blind-75-part-2-4a5d2ed3be91
Mastering Arrays and Strings: A Comprehensive Guide in Python— Blind 75 (Part 2) | by Saquib Khan | Medium
April 22, 2024 - Python’s heapq module provides a min-heap implementation by default. To create a max heap, we can store tuples of negative frequencies and the corresponding number.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappush-method
Python heapq.heappush() Method - GeeksforGeeks
March 11, 2025 - After inserting all elements, the heap is converted back to positive values using list comprehension ([-x for x in h]) to display the correct max-heap order. heapq.heappush() is commonly used in priority queues, where elements are inserted based on their priority. ... import heapq # List of tuples (priority, task) pq= [] # Push elements (priority, task) heapq.heappush(pq, (2, "Task A")) heapq.heappush(pq, (1, "Task B")) heapq.heappush(pq, (3, "Task C")) print("Priority Queue:", pq)
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_heaps.htm
Python - Heaps
If each parent node is greater than or equal to its child node then it is 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.