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)
Answer from gfortune on Stack Overflow
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
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.
Discussions

How to get the largest element from the priority queue?
Put the negative value. Negate again after retrieval. More on reddit.com
๐ŸŒ r/learnpython
6
2
July 4, 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
Peek method in Priority Queue?
The heapq object is a list. So you can peek with standard indexing. I don't think there's a way to peek on a queue.Queue, just due to how they are implemented. More on reddit.com
๐ŸŒ r/learnpython
2
1
September 26, 2020
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - Developers can add either a single value to function as the priority, or a tuple in the form (priority_number, data). A Python tuple is an ordered and immutable list. Similarly to the get method, block and timeout parameters can be passed to the method. The defaults are True and None. If the queue is full, the put method blocks until it times out waiting for a slot to become available.
๐ŸŒ
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 ... in priority order (lower priority numbers are retrieved first). Functions: put(): Puts an item into the queue....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ priority-queue-in-python
Priority Queue in Python - GeeksforGeeks
April 26, 2025 - ... The Queue module is primarily ... object that can take a distinct number of items. The get() and put() methods are used to add or remove items from a queue respectively....
Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - Debug common issues when working ... modification in multi-threaded environments. The put method is used to add tasks to the priority queue, where the first argument is the priority and the second argument is the task itself....
๐ŸŒ
Like Geeks
likegeeks.com โ€บ python-priority-queue
Like Geeks - Linux, Server administration, and Python programming
July 5, 2023 - LikeGeeks - Linux, Server administration, Python programming, and web development tutorials.
๐ŸŒ
Stackify
stackify.com โ€บ a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - The queue.PriorityQueue class is part of Pythonโ€™s queue module and offers a simple way to create thread-safe priority queues. Key Features: Thread safe, making it ideal for multithreaded applications.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - In a Python priority queue, each element is associated and served according to a specific priority. The higher the priority, the sooner the element is served. ... from queue import PriorityQueue # Create a priority queue q = PriorityQueue() ...
๐ŸŒ
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:
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python priority queue: a guide
Python Priority Queue: A Guide | Career Karma
December 1, 2023 - 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 = ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ program for priority queue in python
Program for Priority Queue in Python - Scaler Topics
December 13, 2022 - The first step is to import the PriorityQueue package from the queue module. Create a Python PriorityQueue and give the variable p to the object. Using the put method, create three passengers. Passenger Naman is in business class, which has a priority 2, and passenger Deepika is in first class, ...
๐ŸŒ
Bogotobogo
bogotobogo.com โ€บ python โ€บ python_PriorityQueue_heapq_Data_Structure.php
Python Tutorial: Data Structure - Priority Queue & heapq - 2020
A priority queue is an abstract data type (ADT) which is like a regular queue or stack data structure, but where additionally each element has a priority associated with it. In a priority queue, an element with high priority is served before an element with low priority.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ asyncio-queue.html
Queues โ€” Python 3.14.6 documentation
If a join() is currently blocking, ... than there were items placed in the queue. ... A variant of Queue; retrieves entries in priority order (lowest first)....
๐ŸŒ
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.
๐ŸŒ
Shapehost
shape.host โ€บ home โ€บ resources โ€บ the python priority queue: a comprehensive guide
Understanding Python Priority Queue: A Comprehensive Guide - Shapehost
December 29, 2023 - The default behavior is to block and wait indefinitely for the next item to arrive. maxsize: This method returns the maximum size of the queue. If there is no maximum size, it returns 0. put: This method adds an item with the specified priority to the priority queue.
๐ŸŒ
Python Guides
pythonguides.com โ€บ priority-queue-in-python
Priority Queue in Python
December 12, 2025 - Unlike regular queues that operate in a first-in-first-out (FIFO) manner, priority queues serve elements based on their priority level, the highest priority elements get processed first. In Python, priority queues are often implemented using the heapq module, which provides an efficient min-heap ...
๐ŸŒ
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