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
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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).
๐ŸŒ
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]
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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 ...
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
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-python-priority-queue
What is the Python priority queue?
Python provides a built-in implementation of the priority queue data structure. 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.
๐ŸŒ
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.
๐ŸŒ
Python Guides
pythonguides.com โ€บ priority-queue-in-python
Priority Queue in Python
December 12, 2025 - If your Python application involves multithreading, the queue.PriorityQueue class is a thread-safe implementation of a priority queue.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ priority-queue-in-python
Priority Queue in Python - GeeksforGeeks
April 26, 2025 - The elements of the pri ... A heap queue or priority queue is a data structure that allows us to quickly access the smallest (min-heap) or largest (max-heap) element. A heap is typically implemented as a binary tree, where each parent node's ...
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - The priority queue is instantiated with priority_queue = []. Elements can be added with syntax such as, heapq.heappush(priority_queue, (2, 'task 2')). In a Python priority queue, each element is associated and served according to a specific priority.
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ implementing a priority queue in python
Implementing a Priority Queue in Python: A Comprehensive Guide
December 29, 2025 - Python's queue.PriorityQueue is a thread-safe priority queue that can be used in multi-threaded applications.
Price ย  $
Call ย  +1 844 286 2130
Address ย  1999 Harrison St 1800 9079, 94612, Oakland
๐ŸŒ
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 - Using queue.PriorityQueue: This approach supports concurrent processes and itโ€™s a class interface. The 2nd one โ€” heapq, is preferred. ... import heapq # also a built-in module in python, and this is min-heap.
๐ŸŒ
Shapehost
shape.host โ€บ home โ€บ resources โ€บ the python priority queue: a comprehensive guide
Understanding Python Priority Queue: A Comprehensive Guide - Shapehost
December 29, 2023 - Python takes care of inserting and removing entries based on their priority and maintains the internal structure of the queues. The PriorityQueue class in Python always removes and returns the highest-priority item from the queue. If two items have the same priority, the item that arrived first ...
๐ŸŒ
GitHub
github.com โ€บ abadger โ€บ priorityqueue
GitHub - abadger/priorityqueue: Tests and timings of Python3 Priority Queue implementations
The internet seems to say that the easiest way to implement a Priority Queue in Python is with the queue.PriorityQueue class and the fastest code is made using the heapq module.
Author ย  abadger