Priority Queue is an extension of the queue with the following properties.
- An element with high priority is dequeued before an element with low priority.
- If two elements have the same priority, they are served according to their order in the queue.
It is a constructor for a priority queue. maxsize is the number of elements which can be inserted into queue, its default value is 0. If the maxsize value is less than or equal to 0, then queue size is infinite. Items are retrieved priority order (lowest first).
- put() – Puts an item into the queue.
- get() – Removes and returns an item from the queue.
- qsize() – Returns the current queue size.
- empty() – Returns True if the queue is empty, False otherwise. It is equivalent to qsize()==0.
- full() – Returns True if the queue is full, False otherwise. It is equivalent to qsize()>=maxsize.
qsize() are not reliable as they risk race condition where the queue size might change.
(1, 'e') (2, 'g') Items in queue : 3 Is queue empty : False Is queue full : False
Heapdict implements the MutableMapping ABC, meaning it works pretty much like a regular Python dictionary. It’s designed to be used as a priority queue. Along with functions provided by ordinary
dict(), it also has
peekitem() functions which return the pair with the lowest priority. Unlike heapq module, the HeapDict supports efficiently changing the priority of an existing object (“decrease-key” ). Altering the priority is important for many algorithms such as Dijkstra’s Algorithm and A*.
- clear(self) – D.clear() -> None. Remove all items from D.
- peekitem(self) – D.peekitem() -> (k, v), return the (key, value) pair with lowest value; but raise KeyError if D is empty.
- popitem(self) – D.popitem() -> (k, v), remove and return the (key, value) pair with lowest value; but raise KeyError if D is empty.
- get(self, key, default=None) – D.get(k[, d]) -> D[k] if k in D, else d. d defaults to None.
- items(self) – D.items() -> a set-like object providing a view on D’s items
- keys(self) – D.keys() -> a set-like object providing a view on D’s keys
- values(self) – D.values() -> an object providing a view on D’s values
list of key:value pairs in h: [('g', 2), ('e', 1), ('k', 3), ('s', 4)] pair with lowest priority: ('e', 1) list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ('e', 1) get value for key 5 in h: Not found 
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Heap and Priority Queue using heapq module in Python
- Difference between Circular Queue and Priority Queue
- Stack and Queue in Python using queue Module
- How to implement stack using priority queue or heap?
- Priority Queue using Linked List
- Priority Queue using doubly linked list
- Huffman Coding using Priority Queue
- Find the K closest points to origin using Priority Queue
- Priority Queue using Binary Heap
- Priority Queue in Python
- Multithreaded Priority Queue in Python
- Why is Binary Heap Preferred over BST for Priority Queue?
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- Double ended priority queue
- Priority Queue in C++ Standard Template Library (STL)
- Difference between queue.queue vs collections.deque in Python
- Python | Priority key assignment in dictionary
- Python - Combine dictionary with priority
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.