A built-in priority queue implementation in Python
This example is taken from Python Tricks [2].
import heapq
q = []
heapq.heappush(q, (2, 'code'))
heapq.heappush(q, (1, 'eat'))
heapq.heappush(q, (3, 'sleep'))
while q:
next_item = heapq.heappop(q)
print(next_item)
# Result:
# (1, 'eat')
# (2, 'code')
# (3, 'sleep')```
[2] Python Tricks: A Buffet of Awesome Python Features by Dan Bader
[3] Effective Python: 90 Specific Ways to Write Better Python by Brett Slatkin
[4] Python Cookbook, Third Edition by David Beazley and Brian K. Jones