Python – What do I use for a max-heap implementation in Python

data structuresheappythonrecursive-datastructures

Python includes the heapq module for min-heaps, but I need a max heap. What should I use for a max-heap implementation in Python?

Best Answer

The easiest way is to invert the value of the keys and use heapq. For example, turn 1000.0 into -1000.0 and 5.0 into -5.0.