Creating a python priority Queue
Question: I would like to build a priority queue in python in which the queue contains
different dictionaries with their priority numbers. So when a "get function"
is called, the dictionary with the highest priority(lowest number) will be
pulled out of the queue and when "add function" is called, the new dictionary
will be added to the queue and sorted based on its priority number.

Please do help out...

Thanks in advance!


Answer: Use the heapq module in the standard library.

You don't specify how you wanted to associate priorities with dictionaries,
but here's a simple implementation:

    
    
    import heapq
    
    class MyPriQueue(object):
        def __init__(self):
            self.heap = []
    
        def add(self, d, pri):
            heapq.heappush(self.heap, (pri, d))
    
        def get(self):
            pri, d = heapq.heappop(self.heap)
            return d
    

