Priority Queue in Python Last Updated : 26 Apr, 2025 Comments Improve Suggest changes Like Article Like Report A priority queue is like a regular queue, but each item has a priority. Instead of being served in the order they arrive, items with higher priority are served first. For example, In airlines, baggage labeled “Business” or “First Class” usually arrives before the rest. Key properties of priority queue:High-priority elements are dequeued before low-priority ones.If two elements have the same priority, they are dequeued in their order of insertion like a queue.Key differences between priority queue and queueUnderstanding the difference between these two structures makes it easier to choose the right one for situations like scheduling tasks, managing resources, or solving problems in programs.FeatureRegular QueuePriority QueueOrder of ProcessingFirst-In-First-Out (FIFO)Based on priorityElement Dequeue OrderIn order of arrivalHighest priority firstHandling Same PriorityBased on arrival timeBased on arrival time (if priority is same)Sorting EffectNo sortingActs like a sorted structure when dequeuedBelow is a simple implementation of the priority queue. Python def insert(q, d): q.append(d) def delete(q): try: m = 0 for i in range(len(q)): if q[i] > q[m]: m = i item = q[m] del q[m] return item except IndexError: print("Queue empty.") exit() def is_empty(q): return len(q) == 0 if __name__ == '__main__': q = [] insert(q, 12) insert(q, 1) insert(q, 14) insert(q, 7) print(q) print("Removed elements:") while not is_empty(q): print(delete(q)) Output[12, 1, 14, 7] Removed elements: 14 12 7 1 Explanation: insert(q, d) adds element d to the end of the queue q using append().delete(q) finds and removes the highest priority (max value) element from q. If the queue is empty, it prints "Queue empty." and exits.is_empty(q) returns True if the queue q is empty, otherwise False.In the __main__ block while loop is used to repeatedly remove and print the highest priority element using the delete() function until the queue becomes empty.Applications of priority queueLet's understand the applications of a priority queue because they demonstrate how this data structure can be utilized in real-world scenarios to manage tasks efficiently. Here are some key applications:Task Scheduling (Operating Systems) manages tasks by priority, executing high-priority tasks first in real-time systems.Dijkstra's Shortest Path Algorithm uses a priority queue to find the shortest path by selecting the nearest node.Huffman Encoding (Data Compression) combines least frequent symbols using a priority queue to reduce data size.Merging Multiple Sorted Lists merges sorted lists by selecting the smallest element from each list.A Search Algorithm (Pathfinding) prioritizes nodes based on cost to find the shortest path in navigation or games.Types of priority queueLet's understand the different types of priority queues because they define how elements are prioritized and dequeued based on their associated priority. There are two main types:Max Priority Queue: The element with the highest priority is dequeued first. It’s commonly used when you need to process the most important or largest element first.Min Priority Queue: The element with the lowest priority is dequeued first. It’s useful for problems like finding the smallest element or processing tasks with the least urgency first. Comment More infoAdvertise with us Next Article Priority Queue in Python O Omkar Pathak Follow Improve Article Tags : Misc Queue Heap Python DSA priority-queue Python-DSA +3 More Practice Tags : HeapMiscpriority-queuepythonQueue +1 More Similar Reads Multithreaded Priority Queue in Python The Queue module is primarily used to manage to process large amounts of data on multiple threads. It supports the creation of a new queue object that can take a distinct number of items. The get() and put() methods are used to add or remove items from a queue respectively. Below is the list of oper 2 min read Queue in Python Like a stack, the queue is a linear data structure that stores items in a First In First Out (FIFO) manner. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. Operations 6 min read PriorityQueue in Java The PriorityQueue class in Java is part of the java.util package. It implements a priority heap-based queue that processes elements based on their priority rather than the FIFO (First-In-First-Out) concept of a Queue.Key Points:The PriorityQueue is based on the Priority Heap. The elements of the pri 9 min read Heap queue or heapq in Python A heap queue or priority queue is a data structure that allows us to quickly access the smallest (min-heap) or largest (max-heap) element. A heap is typically implemented as a binary tree, where each parent node's value is smaller (for a min-heap) or larger (for a max-heap) than its children. Howeve 7 min read Heap and Priority Queue using heapq module in Python Heaps are widely used tree-like data structures in which the parent nodes satisfy any one of the criteria given below. The value of the parent node in each level is less than or equal to its children's values - min-heap.The value of the parent node in each level higher than or equal to its children' 5 min read Python | os.sched_get_priority_min() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. OS module contains some methods which provides an interface to the scheduler and 3 min read Applications of Priority Queue A Priority Queue is different from a normal queue, because instead of being a "first-in-first-out", values come out in order by priority. It is an abstract data type that captures the idea of a container whose elements have "priorities" attached to them. An element of highest priority always appears 2 min read Turn a Queue into a Priority Queue What is Queue?Queue is an abstract data type that is open at both ends. One end is always used to insert data (enqueue) which is basically the rear/back/tail end and the other which is the front end is used to remove data (dequeue). Queue follows First-In-First-Out (FIFO) methodology, i.e., "the dat 9 min read OrderedDict in Python OrderedDict is a subclass of Python's built-in dictionary dict that remembers the order in which keys are inserted. Unlike older versions of Python where dictionaries did not guarantee order, OrderedDict preserves insertion order reliably.Note: From Python 3.7 onwards, the built-in dict also preserv 7 min read Python if OR In Python, if statement is the conditional statement that allow us to run certain code only if a specific condition is true . By combining it with OR operator, we can check if any one of multiple conditions is true, giving us more control over our program.Example: This program check whether the no i 2 min read Like