Python 3.14+heapqfunction
heapq.heapify_max()
operations
Turns a list into a max-heap in place, so the largest item ends up at heap[0]. It is the starting point for the other _max functions.
Syntax
heapq.heapify_max(x) -> None
Added in Python 3.14; before that a max-heap was faked by pushing negated numbers (-x) into a regular heap and flipping the sign back on pop, a trick that does not work for strings or objects at all. The function returns None, not the heap: x = heapify_max(data) gives None. The list is not sorted afterwards, only the root is in place, and mixing in heappush/heappop (the min-heap functions) silently breaks the invariant.
Examples
import heapq
data = [3, 1, 4, 1, 5]
print(heapq.heapify_max(data)) # → None
print(data[0]) # → 5
import heapq
# старый способ до 3.14: отрицательные числа
old = [-x for x in [3, 1, 4]]
heapq.heapify(old)
print(-old[0]) # → 4
See also
Open in the full glossarydocs.python.orgReport an inaccuracy