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 › queue.html
queue — A synchronized queue class
February 23, 2026 - Queue objects (Queue, LifoQueue, or PriorityQueue) provide the public methods described below.
🌐
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 - queue.PriorityQueue is a constructor to create a priority queue, where items are stored in priority order (lower priority numbers are retrieved first).
🌐
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.
🌐
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 ...
🌐
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...
🌐
Medium
medium.com › @2019077_13406 › priority-queue-v-s-heapq-python-a4858c0191ac
Priority Queue V/s Heapq Python. Lately, I have been solving LeetCode… | by kartik chuphal | Medium
December 15, 2024 - While implementing a solution with a priority queue, I came across two different implementation options in Python. Let’s dive into what each of them offers. Let’s just test which is faster. import time from queue import PriorityQueue, Empty import heapq def p_queue(): prior_queue = PriorityQueue() jobs = [(x, f"This is item: {x}") for x in range(1, 10 ** 5 + 1)] for job in jobs: prior_queue.put(job) while 1: try: popped_item = prior_queue.get_nowait() except Empty: break def heap_queue(): jobs = [(x, f"This is item: {x}") \ for x in range(1, 10 ** 5 + 1)] heapq.heapify(jobs) for _ in range
Find elsewhere
🌐
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 ...
🌐
Linode
linode.com › docs › guides › python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - This guide discusses priority queues and the PriorityQueue class in Python 3. This data structure allows you to sort items in a queue and return them in a desired order.
🌐
GitHub
github.com › ajscheff › python_pq
GitHub - ajscheff/python_pq: A python priority queue
The simple_test() method very simply demonstrates the purpose of a priority queue. In this queue, lower numbers for priority come first. Run the test with python pq.py and notice how no matter the order that we insert items into the queue, they always come out in the order "A", "B", "C".
Starred by 4 users
Forked by 4 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › mjwestcott › priorityqueue
GitHub - mjwestcott/priorityqueue: A priority queue implementation in Python with a O(log n) remove method · GitHub
A priority queue implementation in Python with a O(log n) remove method - mjwestcott/priorityqueue
Author   mjwestcott
🌐
Real Python
realpython.com › queue-in-python
Python Stacks, Queues, and Priority Queues in Practice – Real Python
December 1, 2023 - Because Python’s heap is a min-heap, its first element always has the lowest value. To fix this, you can flip the sign of a priority when pushing a tuple onto the heap, making the priority a negative number so that the highest one becomes the lowest: ... # queues.py # ... class PriorityQueue: def __init__(self): self._elements = [] def enqueue_with_priority(self, priority, value): heappush(self._elements, (-priority, value)) def dequeue(self): return heappop(self._elements)[1]
🌐
DigitalOcean
digitalocean.com › community › tutorials › priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - A priority queue in Python is a data structure that allows elements to be added and removed based on their priority. It is a type of queue where each element is associated with a priority, and elements are removed in order of their priority. In Python, priority queues can be implemented using ...
🌐
Blogboard
blogboard.io › blog › knowledge › priority-queue-in-python
Priority Queue in Python
April 9, 2024 - Priority queue is a data structure similar to a regular queue, but where elements have priorities assigned. Python comes with a built-in PriorityQueue class contained in the queue module.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python queue.priorityqueue methods
Python queue.priorityqueue Methods - Spark By {Examples}
May 31, 2024 - We can implement the Priority queue using Python queue.priorityqueue methods. Priorityqueue is similar to Queue but it will remove items from it based on Priority. Fewer priority items will never be removed first.
🌐
Wikipedia
en.wikipedia.org › wiki › Priority_queue
Priority queue - Wikipedia
November 10, 2025 - Python's · heapq module implements a binary min-heap on top of a list. Java's library contains a PriorityQueue (java.util.PriorityQueue) class, which implements a min-priority-queue as a binary heap. .NET's library contains a System.Collections.Generic.PriorityQueue class, which implements an array-backed, quaternary min-heap.
🌐
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.

🌐
Python
docs.python.org › 3 › library › asyncio-queue.html
Queues — Python 3.14.4 documentation
February 22, 2026 - class asyncio.PriorityQueue¶ · A variant of Queue; retrieves entries in priority order (lowest first). Entries are typically tuples of the form (priority_number, data). class asyncio.LifoQueue¶ · A variant of Queue that retrieves most recently ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › priority-queue-in-python
Priority Queue in Python - GeeksforGeeks
April 26, 2025 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.