๐ŸŒ
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.
๐ŸŒ
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 ...
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
Could std::collections::PriorityQueue have an iterator to visit elements in priority order?

Binary heaps aren't particularly easy to run through in order without modifying the heap.

More on reddit.com
๐ŸŒ r/rust
7
9
January 25, 2015
Is the queue in an implementation of a LRU cache a priority queue?
It's not necessary to have a priority queue, you can do it with a regular queue (and thus in expected constant time per operation), but the queue has to be implemented as a bidirectional linked list - i.e., each node of the queue stores pointers to both the previous and the next node in the queue. At any moment, the queue will contain the elements in your LRU cache in the order in which they were most recently used. The trick is how to maintain the order. The answer is that in your main hashmap you will store, for each key, not just the cached value but also a pointer into the queue. Whenever you access an element that's already in your cache, you can use that pointer to find it and remove it from the queue. Then, you reinsert it at the "most recent" end of the queue and you update the pointer to it. More on reddit.com
๐ŸŒ r/algorithms
10
1
October 5, 2022
๐ŸŒ
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 ...
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - A Python priority queue always removes and returns the highest-priority item in the queue. If two items have the same priority, Python removes the item that arrived first. For a tuple having both priority and data fields, Python first compares ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-python-priority-queue
What is the Python priority queue?
Since the queue.PriorityQueue class needs to maintain the order of its elements, a sorting mechanism is required every time a new element is enqueued. Python solves this by using a binary heap to implement the priority queue.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ implementing a priority queue in python
Implementing a Priority Queue in Python: A Comprehensive Guide
December 29, 2025 - Learn how to implement and use priority queues in Python with this guide. Discover code examples using `heapq` and `queue.PriorityQueue`.
Price ย  $
Call ย  +1 844 286 2130
Address ย  1999 Harrison St 1800 9079, 94612, Oakland
Find elsewhere
๐ŸŒ
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 queue, but where each element has an associated priority. A queue is a first in, first out (FIFO) data structure, whereas in a priority queue the element with the highest priority is served before ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - Learn how to implement a priority queue in Python using heapq, queue.PriorityQueue, and custom classes. Includes real examples and code.
๐ŸŒ
Readthedocs
boltons.readthedocs.io โ€บ en โ€บ latest โ€บ queueutils.html
queueutils - Priority queues โ€” boltons 25.0.0 documentation
The queueutils module currently provides two Queue implementations: HeapPriorityQueue, based on a heap, and SortedPriorityQueue, based on a sorted list. Both use a unified API based on BasePriorityQueue to facilitate testing the slightly different performance characteristics on various application use cases. >>> pq = PriorityQueue() >>> pq.add('low priority task', 0) >>> pq.add('high priority task', 2) >>> pq.add('medium priority task 1', 1) >>> pq.add('medium priority task 2', 1) >>> len(pq) 4 >>> pq.pop() 'high priority task' >>> pq.peek() 'medium priority task 1' >>> len(pq) 3
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ asyncio-queue.html
Queues โ€” Python 3.14.4 documentation
February 22, 2026 - Entries are typically tuples of the form (priority_number, data). ... A variant of Queue that retrieves most recently added entries first (last in, first out).
๐ŸŒ
Stackify
stackify.com โ€บ a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - This custom priority queue is built using Pythonโ€™s heapq module but encapsulated within a custom class. The push() method adds a new element with a specified priority to the heap. The pop() method removes and returns the element with the highest priority. The peek() method allows us to view the highest priority element without removing it.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - However, in a Priority Queue, this order is determined by the priority of the elements. So, an element that enters the queue later could leave earlier if its priority is higher. Python offers a built-in PriorityQueue class in the queue module.
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
๐ŸŒ
Python Guides
pythonguides.com โ€บ priority-queue-in-python
Priority Queue in Python
December 12, 2025 - In many real-world applications ... priorities can change. Optimize performance with O(log n) insertion and removal. The heapq module is the go-to solution in Python for priority queues....
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - Alternatively, you could ignore the element order until removing one with the highest priority, which you could find using the linear search algorithm. Looking up an element in an unordered list has O(n) time complexity. Sorting the entire queue would be even more expensive, especially when exercised often. Pythonโ€™s list.sort() method employs an algorithm called Timsort, which has O(n log(n)) worst-case time complexity.
๐ŸŒ
Codefellows
codefellows.github.io โ€บ sea-python-401d2 โ€บ assignments โ€บ priority_queue.html
Implement a Priority Queue in Python โ€” Python Dev Accelerator 2.0 documentation
A Priority Queue is similar to a queue, except that in addition to a value, each item in the queue has a โ€œpriorityโ€. When you pop an item off of the queue, you always get the highest priority item. Create a new branch of your data structures repository. Call it priorityq.
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ choosing-priority-queue
Choosing a Priority Queue (Video) โ€“ Real Python
For a thread safe implementation, queue.PriorityQueue is an implementation of heapq with atomic locking. Here are resources and additional documentation about bisect, sorting algorithms, heapq, and PriorityQueue: bisect โ€“ Array bisection algorithm | Python Documentation
Published ย  May 11, 2021