You can use Queue.PriorityQueue.

Recall that Python isn't strongly typed, so you can save anything you like: just make a tuple of (priority, thing) and you're set.

Answer from Charlie Martin on Stack Overflow
🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Source code: Lib/heapq.py This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. Min-heaps are binary trees for which every parent node has ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › priority-queue-in-python
Priority Queue in Python - GeeksforGeeks
April 26, 2025 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 26 Apr, 2025 · A priority queue is like a regular queue, but each item has a priority.
🌐
Built In
builtin.com › data-science › priority-queues-in-python
Introduction to Priority Queues in Python | Built In
Summary: A priority queue in Python allows elements to be processed based on assigned priority rather than arrival order. It can be implemented using lists, the heapq module for efficiency, or the thread-safe PriorityQueue class for concurrent ...
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › if you love queuing, will you also love priority queuing? • [club]
If You Love Queuing, Will You Also Love Priority Queuing? • [Club]
December 15, 2025 - We’ll use the list just as the structure to hold the data, but we’ll rely on another tool for the fun stuff. It’s time to import the heapq module, which is part of the Python standard library: ... This module contains the tools to create and manage a heap queue, which is also known as a priority queue.
🌐
Medium
stephanekp.medium.com › priority-queues-from-scratch-for-dummies-in-under-5-minutes-c3d74468213f
Priority Queues. From scratch. For Dummies. In under 5 Minutes. | by Stephane K. | Medium
July 6, 2022 - What heapsort above does is use the priority queue to extract the max and rebuild the heap so all its elements abide by the heap property. Now you could build a proper API for it (with insert, find, delete, peek,…), but for my use case and given the time I had, heapify is already doing most of the work. I just need to use it and be clever about it. Now I was able to look up this pesky import. And imagine what? As I should have expected from the zen of python, it’s as simple as from queue import PriorityQueue.
🌐
Stackify
stackify.com › a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - The queue.PriorityQueue class in Python’s standard library is a straightforward way to implement a priority queue. It’s built on top of a heap and offers thread-safe operations, making it suitable for multithreaded programs.
🌐
UCI ICS
ics.uci.edu › ~pattis › common › modules › courselibdoc › priorityqueue.html
Python: module priorityqueue
Classes builtins.object PriorityQueue · class PriorityQueue(builtins.object) Implements a Priority Queue data type: values are removed according to the highest priority value first rule. Methods defined here: __add__(self, x)Overload syntax: pq = pq + x is the same as pq.add(x) __bool__(s...
Find elsewhere
🌐
Python
docs.python.org › 3 › library › queue.html
queue — A synchronized queue class
In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). With a priority queue, the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first.
🌐
Medium
medium.com › @kapilsharmax24 › what-is-a-priority-queue-21badf357301
What is a Priority Queue ?. A priority queue is a special type of… | by Shivam | Medium
September 21, 2024 - Unlike regular queues where elements are processed in the order they arrive (FIFO), in a priority queue, elements are processed based on their priority. The highest-priority element is always retrieved first.
🌐
Akamai
akamai.com › cloud › guides › python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - However, it is often necessary to account for the priority of each item when determining processing order. A queue that retrieves and removes items based on their priority as well as their arrival time is called a priority queue.
🌐
DigitalOcean
digitalocean.com › community › tutorials › priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - A priority queue stores (priority, item) pairs so the element with the highest priority (or lowest, for min-heap) is removed first. Python ships two ready-made solutions: heapq and queue.PriorityQueue.
🌐
Medium
medium.com › @huawei.zhu › built-in-stack-queue-and-priority-queue-in-python-e44a6cbf3771
Built-in stack, queue and priority queue in Python | by Huawei Zhu | Medium
September 2, 2025 - # To create a max-heap, # just make every item the opposite sign when you push them into the heap #======================== # Test 1: heapify a list #======================== a = [3, 5, 1, 2, 6, 8, 7] heapq.heapify(a) # this turns the list to a priority queue print("a =", a) # note the difference between a and a after heapify() #======================== # Test 2: initialize with an empty list #======================== p_queue = [] # note that you will get an error if you do p_queue = heapq() # use heappush() heapq.heappush(p_queue, 10) heapq.heappush(p_queue, 5) heapq.heappush(p_queue, 20) print(p_queue) # Regardless of the order elements are pushed into heapq, # it always pops the smallest element.
🌐
GeeksforGeeks
geeksforgeeks.org › python › priority-queue-using-queue-and-heapdict-module-in-python
Priority Queue using Queue and Heapdict module in Python - GeeksforGeeks
January 8, 2026 - A Priority Queue is a special type of queue where elements with higher priority are dequeued before elements with lower priority.
🌐
YouTube
youtube.com › watch
Priority Queue Data Structure in Python: Coding Tutorials by Umar Khan - YouTube
In this video, we'll talk about the priority queue and create one in Python. A priority queue is like a queue, but there are some differences. In a priority ...
Published   October 15, 2022
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 298434 › python-priority-queue
python priority queue [SOLVED] | DaniWeb
July 22, 2010 - In other words, you must enqueue a value that carries the priority with it. Also note that PriorityQueue returns the lowest-valued item first; the common pattern is to push a tuple like (priority, data). In Python 2.x the module is Queue; in Python 3.x it is queue.
🌐
Reddit
reddit.com › r/python › is there a better priority queue?
r/Python on Reddit: Is there a better priority queue?
November 21, 2017 -

I know python has heapq and queue.priorityqueue but honestly, both of them are really cumbersome compared to Java's priorityqueue. I find it tedious to have to insert a tuple, with the first element in the tuple defining the priority. Also, it makes it hard to write more complex comparisons. Is there a way we can pass in a comparator to the Priorityqueue? I know it's possible to define classes with their own comparator method, but again, this is really tedious and I'm looking for something as close as possible to Java's PQ.

🌐
Hostman
hostman.com › tutorials › implementing-a-priority-queue-in-python
Implementing a Priority Queue in Python: A Comprehensive Guide
Priority queues are essential for efficiently managing tasks and resources based on priority. Python's heapq and queue.PriorityQueue modules provide powerful tools to implement and manipulate priority queues.
🌐
Plain English
python.plainenglish.io › i-can-teach-you-priority-queue-2799fb832e02
I Can Teach You ‘Priority Queue’. A Priority Queue is a specialized data… | by Kapil Bhardwaj | Python in Plain English
October 29, 2024 - ... A Priority Queue is a specialized data structure in computer science that operates similarly to a standard queue but with a unique twist: then each of them is associated with a priority level, at which indicates the order in which the components are considered.