Queue.PriorityQueue vs heapq for priority queues in python?
Anonymous User
1566

Which do you prefer to use in interviews?

https://docs.python.org/3/library/heapq.html

import heapq

H = [21,1,45,78,3,5]
# Create the heap

heapq.heapify(H)
print(H)

# Remove element from the heap
heapq.heappop(H)

https://docs.python.org/3/library/queue.html#queue.PriorityQueue

from queue import PriorityQueue

q = PriorityQueue()

q.put(4)
q.put(2)
q.put(5)
q.put(1)
q.put(3)

while not q.empty():
    next_item = q.get()
    print(next_item)
Comments (1)