12/40
Part 3 of 8 · heapqLearn and do

For a max-heap of numbers on Python 3.12, push the negative

In Python 3.12, every public heapq function works on a min-heap. For numbers, push the negative, and negate again on the way out:

scores = {"ana": 91, "bo": 78, "cy": 95, "di": 88}
h = [(-score, name) for name, score in scores.items()]
heapq.heapify(h)
neg, name = heapq.heappop(h)
print((-neg, name))
Predict

Scores ana 91, bo 78, cy 95, di 88 go in as (-score, name). What comes out of the first pop, negated back?

Pick one to see what it prints.
Pick an answer above to see the rest