heapq
Use a list as a min-heap: heap[0] is the smallest item, while the rest of the
list is not necessarily sorted.
Items must support ordering. Push and pop operations take logarithmic time.
This module provides these five functions; CPython helpers such as merge,
nlargest, and nsmallest are not included.
import heapq
jobs = [(3, 'save'), (1, 'input'), (2, 'update')]
heapq.heapify(jobs)
heapq.heappush(jobs, (0, 'network'))
assert heapq.heappop(jobs) == (0, 'network')
assert heapq.heappop(jobs) == (1, 'input')
Tuple priorities compare subsequent fields to break ties. Add a numeric sequence number if tied jobs contain objects that cannot be compared.
Implementation: heapq.py.