Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 920 Bytes

heapq.md

File metadata and controls

35 lines (27 loc) · 920 Bytes

heapq [Performance, Programming Efficiency]

Detectors

What is this?

A built-in priority queue implementation in Python

Example

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')```

References

[1] Python docs -- heapq

Books that mention this topic:

[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