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
Discussions

How does heap pop works when popping tuple items?
Yes, that's correct. And it's not just heaps; python compares all collections of things that way in all contexts. >>> sorted([(1, 99), (1, 10)]) [(1, 10), (1, 99)] >>> sorted(["ab", "aa"]) ['aa', 'ab'] More on reddit.com
🌐 r/learnpython
5
1
August 3, 2023
Confused by heapq's behavior regardring tuples.
h_ok works as expected giving [(-1, -10), (-1, -9), (-1, -7)] but h_wtf gives [(-1, -10), (-1, -2), (-1, -7)] Isn't that correct? print(h_wtf) is not supposed to print all the heap elements in their heappop()-ping order, but rather to output the internal representation of the heap (which is a list). And for min heaps, the only condition is that children are greater than their parent(s); it's definitively true in your case: both -2 and -7 are greater than -10. More on reddit.com
🌐 r/learnpython
14
6
November 5, 2025
Python heapq.heappush for tuple not working as expected - Stack Overflow
As the title stated, I'm a little confused on the implementation of heapq in python. Lets say I have an array of tuples: inputArr = [(-8, 505), (-9, 333), (-6, 2), (-7, 94), (-2, 3), (-3, 101),... More on stackoverflow.com
🌐 stackoverflow.com
How do you perform heapify on a list of tuples

To make a heap based on the first (0 index) element:

import heapq
heapq.heapify(A)

If you want to make the heap based on a different element, you'll have to make a wrapper class and define the __cmp__() method.

More on reddit.com
🌐 r/learnpython
3
4
April 8, 2020
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › if you love queuing, will you also love priority queuing? • [club]
If You Love Queuing, Will You Also Love Priority Queuing? • [Club]
December 15, 2025 - It’s not a data type—you don’t create an instance of type heapq as you would with data structures. You use a list as the data structure, which is why you pass the list service_queue as the first argument to .heappush(). The second argument is the item you want to push to the heap. In this case, it’s the tuple (2, “Jim”).
🌐
GeeksforGeeks
geeksforgeeks.org › python › heapq-with-custom-predicate-in-python
Heapq with custom predicate in Python - GeeksforGeeks
July 23, 2025 - The dictionary items can be converted into a list of tuples and then passed to the heapify method. ... #import module import heapq as hq # the dictionary to be as heap my_dict = {'z': 'zebra', 'b': 'ball', 'w': 'whale', 'a': 'apple', 'm': 'monkey', ...
🌐
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?

🌐
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.
🌐
Interviewcrunch
interviewcrunch.com › python › advanced-data-structures › heap
Heap | InterviewCrunch: Coding Interviews Broken Down
If tuples are stored in the heap, heapq will attempt to arrange the items based on the first values of the tuples, and then the second values if the first values are equivalent.
Find elsewhere
🌐
Coderz Column
coderzcolumn.com › tutorials › python › heapq-heap-queue-priority-queue-implementation-in-python
heapq - Heap Queue / Priority Queue Implementation in Python by Sunny Solanki
February 10, 2021 - It takes the first element of the tuple to create a heap. This can be helpful in situations where we are some important number like priority present as the first element of the item. import heapq import random random.seed(123) heap = ...
🌐
Reddit
reddit.com › r/learnpython › confused by heapq's behavior regardring tuples.
r/learnpython on Reddit: Confused by heapq's behavior regardring tuples.
November 5, 2025 -

Was doing some leetcode problems when i encountered some weird behavior i can't make sense of.

   arr_wtf = [2,7,10]
    h_wtf = []
    for n in set(arr_wtf):
        heappush(h_wtf, (arr_wtf.count(n)*-1, n*-1))
    print(h_wtf)
    arr_ok = [7,10,9]
    h_ok = []
    for n in set(arr_ok):
        heappush(h_ok, (arr_ok.count(n)*-1, n*-1))
    print(h_ok)

Above is the minimalist version to illustrate whats confusing me.

What it should do is fill the heap with tuples of count and value and order them (thus the multiply by minus one.

h_ok works as expected giving [(-1, -10), (-1, -9), (-1, -7)]
but h_wtf gives [(-1, -10), (-1, -2), (-1, -7)]

Notice the -2 between -10 and -7
In case of a tie heapq should look up the next value inside a tuple.
Shouldn't the order of h_wtf be [(-1, -10), (-1, -7), (-1, -2)] ?

Hope you guys can understand what im trying to describe.

Related leecode problem is:
3318. Find X-Sum of All K-Long Subarrays I

🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-and-priority-queue-using-heapq-module-in-python
Heap and Priority Queue using heapq module in Python - GeeksforGeeks
July 23, 2025 - The priority queue is implemented in Python as a list of tuples where the tuple contains the priority as the first element and the value as the next element. ... Consider a simple priority queue implementation for scheduling the presentations ...
🌐
Python Pool
pythonpool.com › home › blog › python heapq: boost your efficiency with heap operations!
Python Heapq: Boost Your Efficiency with Heap Operations!
May 30, 2023 - A tuple is a data structure that consists of two or more values stored in memory at once, ordered in a particular way. Python’s heapq is a higher-level way of handling tuples. Heapq is a way to organize and work with Python dictionaries and ...
🌐
APXML
apxml.com › courses › data-structures-algorithms-ml › chapter-5-heaps-priority-queues-ml › python-heapq
Python heapq Module for Heap Operations
The standard techniques involve ... When you pop, negate the result back. Use Tuples: Store tuples where the first element is the negated priority (or priority multiplied by -1) and the second element is the actual item....
🌐
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
From what I understand from Python's documentation, when I use heappush into an array called arr the elements should be sorted based on the first element on the first tuple.
🌐
YouTube
youtube.com › codeflare
python heapify list of tuples - YouTube
Download this code from https://codegive.com Certainly! Heapifying a list of tuples in Python involves arranging the tuples in a way that satisfies the heap ...
Published   December 25, 2023
Views   55
🌐
Python
bugs.python.org › issue43385
Issue 43385: heapq fails to sort tuples by datetime correctly - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/87551
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heapify-method
Python heapq.heapify() Method - GeeksforGeeks
June 26, 2026 - heapq.heappop(tasks) returns the tuple with the smallest priority value (1).