Python Flashcards

1
Q

How to use Python’s heap data structure?

A

heapq implements minheap

from heapq import heapify, heappush, heappop
from heapq import heappushpop

l = [1,2,3]
heapify(l)
heappush(l, 4)
min = heappop(l)
min = heappushpop(l, 10)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly