Open this example in the interactive library
Source code
from heapq import heapify, heapreplace
offers = [5, 12, 9]
heapify(offers)
print(heapreplace(offers, 11), sorted(offers))Sample output
5 [9, 11, 12]

Replace the cheapest offer in a fixed-size heap in Python.
Open this example in the interactive library
from heapq import heapify, heapreplace
offers = [5, 12, 9]
heapify(offers)
print(heapreplace(offers, 11), sorted(offers))5 [9, 11, 12]