The heapq Module
The heapq
module uses heap algorithms to keep a list in “nearly sorted” order as items are inserted and extracted. heapq
’s operation is faster than either calling a list’s sort
method after each insertion or using bisect
, and, for many purposes, (such as implementing “priority queues”) the nearly sorted order supported by heapq
may be just as useful as a fully sorted order. Module heapq
supplies the following functions.
heapify |
Permutes |
heappop |
Removes and returns the smallest (first) item of |
heappush |
Inserts the |
heapreplace |
|
Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.