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 โ€” Python 3.14.4 documentation
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 ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class โ€” Python 3.14.4 documentation
February 23, 2026 - 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.
Discussions

python - How to put items into priority queues? - Stack Overflow
In the Python docs, The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: ( More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a way to make Python's built-in PriorityQueue also return the priority number in addition to the actual item?
The PriorityQueue queue uses the sorting order of the items to determine their priority, it doesn't generate any absolute priority number, it uses the relative priority between items. That said you can define a priority number yourself using tuples or wrapping your items in a dataclass as the documentation explains. https://docs.python.org/3/library/queue.html @dataclass(order=True) class PrioritizedItem: priority: int item: Any=field(compare=False) # with this you instantiate prioritized items this way and put them in the queue prioritized = PrioritizedItem(123, my_item) More on reddit.com
๐ŸŒ r/learnprogramming
2
1
March 2, 2022
tips on how to make a linear priority queue?
https://www.educative.io/answers/what-is-the-python-priority-queue More on reddit.com
๐ŸŒ r/learnpython
2
1
January 19, 2023
Is there a better priority queue?
You don't have to give a priority queue a tuple of (priority, value), that's just the most common use case. If you wanted to base priority on 3 variables, you could give (a, b, c, value), where ties between a values are broken by the b values, etc. More on reddit.com
๐ŸŒ r/Python
5
6
November 21, 2017
Top answer
1 of 3
37

Just use the second item of the tuple as a secondary priority if a alphanumeric sort on your string data isn't appropriate. A date/time priority would give you a priority queue that falls back to a FIFIO queue when you have multiple items with the same priority. Here's some example code with just a secondary numeric priority. Using a datetime value in the second position is a pretty trivial change, but feel free to poke me in comments if you're not able to get it working.

Code

import Queue as queue

prio_queue = queue.PriorityQueue()
prio_queue.put((2, 8, 'super blah'))
prio_queue.put((1, 4, 'Some thing'))
prio_queue.put((1, 3, 'This thing would come after Some Thing if we sorted by this text entry'))
prio_queue.put((5, 1, 'blah'))

while not prio_queue.empty():
    item = prio_queue.get()
    print('%s.%s - %s' % item)

Output

1.3 - This thing would come after Some Thing if we didn't add a secondary priority
1.4 - Some thing
2.8 - super blah
5.1 - blah

Edit

Here's what it looks like if you use a timestamp to fake FIFO as a secondary priority using a date. I say fake because it's only approximately FIFO as entries that are added very close in time to one another may not come out exactly FIFO. I added a short sleep so this simple example works out in a reasonable way. Hopefully this helps as another example of how you might get the ordering you're after.

import Queue as queue
import time

prio_queue = queue.PriorityQueue()
prio_queue.put((2, time.time(), 'super blah'))
time.sleep(0.1)
prio_queue.put((1, time.time(), 'This thing would come after Some Thing if we sorted by this text entry'))
time.sleep(0.1)
prio_queue.put((1, time.time(), 'Some thing'))
time.sleep(0.1)
prio_queue.put((5, time.time(), 'blah'))

while not prio_queue.empty():
    item = prio_queue.get()
    print('%s.%s - %s' % item)
2 of 3
32

As far as I know, what you're looking for isn't available out of the box. Anyway, note that it wouldn't be hard to implement:

from Queue import PriorityQueue

class MyPriorityQueue(PriorityQueue):
    def __init__(self):
        PriorityQueue.__init__(self)
        self.counter = 0

    def put(self, item, priority):
        PriorityQueue.put(self, (priority, self.counter, item))
        self.counter += 1

    def get(self, *args, **kwargs):
        _, _, item = PriorityQueue.get(self, *args, **kwargs)
        return item


queue = MyPriorityQueue()
queue.put('item2', 1)
queue.put('item1', 1)

print queue.get()
print queue.get()

Example output:

item2
item1
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - A queue that retrieves and removes items based on their priority as well as their arrival time is called a priority queue. Prioritization can be complicated, but fortunately Python priority queues can be easily and efficiently implemented using ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ priority-queue-in-python
Priority Queue in Python - GeeksforGeeks
April 26, 2025 - 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.
๐ŸŒ
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 - Let's learn how to use Priority Queue in Python with queue.PriorityQueue and heapdict. queue.PriorityQueue is a constructor to create a priority queue, where items are stored in priority order (lower priority numbers are retrieved first).
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
Stackify
stackify.com โ€บ a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - Elements with higher priority are queued before those with lower priority. In cases where two elements have the same priority, they are processed according to their arrival order.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-python-priority-queue
What is the Python priority queue?
The Python priority queue is built on the heapq module, which is basically a binary heap. For insertion, the priority queue uses the put function in the following way: pQueue.put(value) The get command dequeues the highest priority elements from the queue. from queue import PriorityQueue ยท q = PriorityQueue() q.put(4) q.put(2) q.put(5) q.put(1) q.put(3) while not q.empty(): next_item = q.get() print(next_item) Run ยท
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ p โ€บ python-heapq-heap-priority-queue
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.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - A priority queue can be implemented using the Python heapq module. The priority queue is instantiated with priority_queue = []. Elements can be added with syntax such as, heapq.heappush(priority_queue, (2, 'task 2')).
๐ŸŒ
Blogboard
blogboard.io โ€บ blog โ€บ knowledge โ€บ priority-queue-in-python
Priority Queue in Python
April 9, 2024 - Python comes with a built-in PriorityQueue class, contained in the queue module. In the simplest case, an entry in the priority queue will be a tuple (priority_number, data). Here's a dummy example of how to use it: import random from queue ...
๐ŸŒ
dbader.org
dbader.org โ€บ blog โ€บ priority-queues-in-python
Priority Queues in Python โ€“ dbader.org
April 12, 2017 - A priority queue is a container data structure that manages a set of records with totally-ordered keys (for example, a numeric weight value) to provide quick access to the record with the smallest or largest key in the set.
๐ŸŒ
Python Guides
pythonguides.com โ€บ priority-queue-in-python
Priority Queue in Python
December 12, 2025 - Processing ticket: Server down with priority 1 Processing ticket: Billing issue with priority 2 Processing ticket: Password reset with priority 3 ยท This ensures urgent problems get immediate attention. Use tuples (priority, item) to store data, so priority determines order. Remember that heapq implements a min-heap by default. For max-priority queues, invert the priority by storing negative values.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python priority queue: a guide
Python Priority Queue: A Guide | Career Karma
December 1, 2023 - To access the PriorityQueue class, we need to import it into our code, which we can do using this Python import statement: ... Suppose we want to create a priority queue for ticket holders at a local concert. We could do so using this code: from queue import PriorityQueue ticket_holders = PriorityQueue() ticket_holders.put((3, 'Paul')) ticket_holders.put((1, 'Miles')) ticket_holders.put((2, 'Dani')) while not ticket_holders.empty(): item = ticket_holders.get() print(item)
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ implementing a priority queue in python
Implementing a Priority Queue in Python: A Comprehensive Guide
December 29, 2025 - A priority queue is a special type of queue where each element is associated with a priority. In a priority queue, elements are dequeued in order of their priority, rather than their insertion order.
Price ย  $
Call ย  +1 844 286 2130
Address ย  1999 Harrison St 1800 9079, 94612, Oakland
๐ŸŒ
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.
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - Notice that the priority comes before the value to take advantage of how Python compares tuples. Unfortunately, there are a few problems with the above implementation that become apparent when you try to use it: ... >>> from queues import PriorityQueue >>> CRITICAL = 3 >>> IMPORTANT = 2 >>> NEUTRAL = 1 >>> messages = PriorityQueue() >>> messages.enqueue_with_priority(IMPORTANT, "Windshield wipers turned on") >>> messages.enqueue_with_priority(NEUTRAL, "Radio station tuned in") >>> messages.enqueue_with_priority(CRITICAL, "Brake pedal depressed") >>> messages.enqueue_with_priority(IMPORTANT, "Hazard lights turned on") >>> messages.dequeue() (1, 'Radio station tuned in')