heapq
A binary min-heap on top of a list — priority queues in O(log n).
Easy
import heapq
h = []
heapq.heappush(h, (2, "task B")); heapq.heappush(h, (1, "task A"))
heapq.heappop(h) # (1, 'task A')
heapq.nlargest(3, data)
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.