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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappush-method
Python heapq.heappush() Method - GeeksforGeeks
June 11, 2026 - Explanation: Negative values are inserted using heapq.heappush(). The heap is maintained on negative numbers, and [-x for x in h] converts them back to positive values. Example 3: This example uses tuples where the first value represents the priority and the second value represents the task.
🌐
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?

🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Push item on the max-heap heap, then pop and return the largest item from heap. The combined action runs more efficiently than heappush_max() followed by a separate call to heappop_max().
🌐
Interviewcrunch
interviewcrunch.com › python › advanced-data-structures › heap
Heap | InterviewCrunch: Coding Interviews Broken Down
If the items in the heap are tuples, heapify() use the first value in the tuple to arrange by: Run · Actual Output · Standard Output · O(log(n)) Add an item to the heap via the heappush() function: Run · Actual Output · Standard Output · O(log(n)) To remove an item from a heap, use heappop() .
🌐
Medium
medium.com › @prathik.codes › pythons-heapq-a-guide-to-efficient-priority-queues-140b890c48a6
Python’s heapq: A Guide to Efficient Priority Queues | by Prathik C | Medium
January 15, 2026 - tasks = [] heapq.heappush(tasks, ... print(f"Processing: {task}") ... When pushing a tuple like this, the heap is maintained on the basis of the first element of the tuple....
🌐
W3Schools
w3schools.com › python › ref_module_heapq.asp
Python heapq Module
Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises Code Challenge Python Tuples
🌐
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: import heapq employees = [] # Storing as (-salary, name) to create a max-heap on salary heapq.heappush(employees, (-90000, "Alice")) heapq.heappush(employees, (-120000, "Bob")) heapq.heappush(employees, (-80000, "Charlie")) # Popping gives the highest paid employee highest_paid = heapq.heappop(employees) print(highest_paid) # Output: (-120000, 'Bob')
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › heapq-with-custom-predicate-in-python
Heapq with custom predicate in Python - GeeksforGeeks
July 23, 2025 - The heapq module functions can take either a list of items or a list of tuples as a parameter.
🌐
LeetCode
leetcode.com › problems › the-k-weakest-rows-in-a-matrix › solutions › 638548 › python-90-one-liner
The K Weakest Rows in a Matrix - LeetCode
Can you solve this real interview question? The K Weakest Rows in a Matrix - You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the ...
🌐
Medium
dpythoncodenemesis.medium.com › understanding-pythons-heapq-module-a-guide-to-heap-queues-cfded4e7dfca
Understanding Python’s Heapq Module: A Guide to Heap Queues | by Python Code Nemesis | Medium
October 21, 2023 - When elements are added to the heap, the algorithm ensures that the heap property is maintained, which involves comparing and swapping elements at specific positions in the heap. This process is optimized due to the logarithmic nature of the binary heap structure, resulting in the overall time complexity of O(log n) for both heappush and heappop operations. Let’s demonstrate the time complexities with Python examples.
🌐
Medium
medium.com › plain-simple-software › python-heapq-use-cases-and-time-complexity-ee7cbb60420f
Python HeapQ Use Cases and Time Complexity | by Yujian Tang | Plain Simple Software | Medium
August 18, 2022 - Advanced priority queue setups allow you to sort based on more than one attribute. In most cases, priority queues are implemented with heaps. In Python, the heapq module is the Python heap queue library.
🌐
Stack Overflow
stackoverflow.com › questions › 77347798 › python-heapq-heappush-for-tuple-not-working-as-expected
Python heapq.heappush for tuple not working as expected - Stack Overflow
As you can see, the tuples are not sorted based on the first element. I was wondering if this has something to do with the way min-heaps are structured but can't say for sure. If someone could explain this to me I'd forever be grateful. ... They aren't sorted. They form a binary heap ... Save this answer. ... Show activity on this post. Heap only guarantees that you have a minimum element at index 0, which is true as you have -9 at 0 position (https://docs.python.org/3/library/heapq.html#module-heapq).
🌐
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 n…
🌐
Python.org
discuss.python.org › ideas
Provide optional key= and reverse= parameters on the heapq functions - Ideas - Discussions on Python.org
July 16, 2024 - The sorted() built-in and list.sort() both accept an optional key= parameter to specify the key for the sort and reverse= to reverse the result. But the functions in the heapq module do not. This feels like a curious omi…
🌐
Delft Stack
delftstack.com › home › howto › python › max heap python
How to Get Max Heap in Python | Delft Stack
February 2, 2024 - 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.
🌐
You.com
codegrepper.com › code-examples › php › heapq+python+with+tuples
The Leading Web Search APIs for AI
from youdotcom import You from youdotcom.models import ContentsFormats with You("<apiKey>") as you: res = you.contents.generate( urls=[ "https://www.python.org", "https://www.example.com", ], formats=[ContentsFormats.HTML], ) # Access the full page content in your chosen format for page in res: print(f"Title: {page.title}") print(f"HTML: {page.html}...")