Introduction to Priority Queues in Python

A priority queue in Python is an abstract data structure that is like a normal queue but where each item has a special “key” to quantify its “priority.”

Written by Raivat Shah
Chess pieces in a queue with one given priority.
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Brennan Whitfield | Jan 07, 2025

A queue in Python is a data structure that follows a first-in-first-out (FIFO) order, in which items are taken out or accessed on a first-come-first-served basis. An example of a queue would be a line at a movie ticket stand. But what is a priority queue?

A priority queue in Python is an abstract data structure (a data structure defined by its behavior) that is like a normal queue but where each item has a special “key” to quantify its priority. For example, if the movie theater decides to serve loyal customers first, it will order them by their loyalty, either by loyalty points or number of tickets purchased. In such a case, the queue for tickets will no longer be first-come-first-served, but most-loyal-first-served. The customers will be the “items” of this priority queue while the “priority” or “key” will be their loyalty.

3 Ways to Build Priority Queues in Python

  1. Using list: This strategy is efficient if you don’t need to make many insertions.
  2. Using heapq: This version supports O(logn) time for insertion and the smallest element.
  3. Using queue.PriorityQueue: This approach supports concurrent processes and it’s a class interface.

Another example is airlines that put luggage on the conveyor belt based on the status or ticket class of the passengers. Baggage tagged with “priority,” “business” or “first-class” usually arrives earlier than other non-tagged baggage.

More on Data: Sorting Algorithms: Slowest to Fastest

 

An introduction to priority queues in Python. | Video: WilliamFiset

How to Implement Priority Queues in Python

Consider that we want to have a priority queue of customers based on their loyalty points. The higher the points, the higher their priority. When it comes to implementing priority queues in Python, there are a number of options. We will explore three of them here.

1. Using a LIST

A very simple and straightforward way is to use a normal Python list but sort it every time an item is added. Here’s an example:

customers = []
customers.append((2, "Harry")) #no sort needed here because 1 item. 
customers.append((3, "Charles"))
customers.sort(reverse=True) 
#Need to sort to maintain order
customers.append((1, "Riya"))
customers.sort(reverse=True) 
#Need to sort to maintain order
customers.append((4, "Stacy"))
customers.sort(reverse=True)
while customers:
     print(customers.pop(0))
#Will print names in the order: Stacy, Charles, Harry, Riya. 

However, it takes O(n log n) time to maintain the order when an item is added to the list. It’s only efficient when we don’t need to make many insertions.

2. Using HEAPQ

We can also use the heapq module in Python to implement our priority queue. This implementation has O(log n) time complexity for insertion and extraction of the smallest element. Note that heapq only has a min heap implementation, but there are other ways to use a max heap that we won’t cover in this article.

Here’s an example:

import heapq
customers = []
heapq.heappush(customers, (2, "Harry"))
heapq.heappush(customers, (3, "Charles"))
heapq.heappush(customers, (1, "Riya"))
heapq.heappush(customers, (4, "Stacy"))
while customers:
     print(heapq.heappop(q))
#Will print names in the order: Riya, Harry, Charles, Stacy.

3. Using QUEUE.PRIORITYQUEUE

The PriorityQueue class uses the same heapq implementation from the previous example internally, so it has the same time complexity of O(log n). However, it’s different in two key ways. First, it’s synchronized, so it supports concurrent processes. Second, it’s a class interface as opposed to the function-based interface of heapq. Thus, PriorityQueue is the classic object-oriented programming (OOP) style of implementing and using priority queues.

Let’s construct a priority queue for our movie buffs:

from queue import PriorityQueue
customers = PriorityQueue() #we initialise the PQ class instead of using a function to operate upon a list. 
customers.put((2, "Harry"))
customers.put((3, "Charles"))
customers.put((1, "Riya"))
customers.put((4, "Stacy"))
while customers:
     print(customers.get())
#Will print names in the order: Riya, Harry, Charles, Stacy.

And that’s how we can implement priority queues in Python. I hope this article helps you get started with and understand the importance of priority queues.

Frequently Asked Questions

Yes, a priority queue can be implemented in Python by using a list, importing the heapq module or importing the queue module and using the PriorityQueue class. For the heapq module and PriorityQueue class, each comes with functions (for heapq) or objects (for PriorityQueue) that can be used to manage items and other properties of the priority queue.

Yes, the PriorityQueue class in Python is thread-safe, meaning its data can be exchanged between multiple threads without unexpected behavior. In Python, a priority queue implemented by the PriorityQueue class uses locks to temporarily block competing threads and ensure thread safety.

A priority queue can be implemented in Python using various methods, including lists, the heapq module or the PriorityQueue class from the queue module. Each has their own advantages depending on speed, structure and multithreading needs: 

  • Python list: efficient if not needing many element insertions; has O(n log n) time complexity.
  • heapq module: uses a min heap data structure; has O(log n) time complexity.
  • PriorityQueue class: uses a Python class interface and supports thread safety; has O(log n) time complexity.

The heapq module in Python provides a min heap data structure by default, where the root element of the heap is the smallest item.

Explore Job Matches.