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
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ priority-queues-in-python
Introduction to Priority Queues in Python | Built In
... import heapq customers = [] heapq.heappush(customers, (2, "Harry")) heapq.heappush(customers, (3, "Charles")) heapq.heappush(customers, (1, "Riya")) heapq.heappush(customers, (4, "Stacy")) while customers: print(heapq.heappop(customers)) ...
๐ŸŒ
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 - Example: Python ยท from queue import PriorityQueue pq = PriorityQueue() pq.put((2, 'g')) pq.put((3, 'e')) pq.put((4, 'k')) pq.put((5, 's')) pq.put((1, 'e')) print(pq.get()) print(pq.get()) print('Items in queue:', pq.qsize()) print('Is queue ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ priority-queue-in-python
Priority Queue in Python
December 12, 2025 - ... from queue import PriorityQueue pq = PriorityQueue() # Add tasks with priority pq.put((2, "Complete project plan")) pq.put((1, "Respond to urgent emails")) pq.put((3, "Schedule meeting")) # Process tasks while not pq.empty(): priority, task = pq.get() print(f"Handling task: {task} with ...
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
The lowest valued entries are retrieved ... form: (priority_number, data). If the data elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number: from dataclasses import dataclass, field from typing import Any @dataclass(order=True) class PrioritizedItem: priority: int item: Any=field(compare=False)...
๐ŸŒ
Blogboard
blogboard.io โ€บ blog โ€บ knowledge โ€บ priority-queue-in-python
Priority Queue in Python
April 9, 2024 - Here's a dummy example of how to use it: import random from queue import PriorityQueue def generate_items(): items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] priorities = [2, 1, 3, 3, 0, 1, 1, 2] return items, priorities if __name__ == '__main__': ...
๐ŸŒ
Stackify
stackify.com โ€บ a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - ... from queue import PriorityQueue # Create a PriorityQueue instance pq = PriorityQueue() # Adding elements with priorities (priority, value) pq.put((2, "Task B")) pq.put((1, "Task A")) pq.put((3, "Task C")) # Removing elements while not ...
Find elsewhere
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - The example in this section ... entries, and how to remove all remaining items from the queue. Import the PriorityQueue package from 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
๐ŸŒ
Shapehost
shape.host โ€บ home โ€บ resources โ€บ the python priority queue: a comprehensive guide
Understanding Python Priority Queue: A Comprehensive Guide - Shapehost
December 29, 2023 - Hereโ€™s an example that demonstrates how to use it: from queue import PriorityQueue # Create a priority queue q = PriorityQueue() # Add passengers to the queue q.put((2, "Smith")) # Business class q.put((1, "Jones")) # First class q.put((4, "Wilson")) # Standby class # Remove the highest priority ...
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ implementing-a-priority-queue-in-python
Implementing a Priority Queue in Python: A Comprehensive Guide
... import heapq priority_queue = [(2, 'task 2'), (1, 'task 1'), (3, 'task 3')] heapq.heapify(priority_queue) # Remove task 2 and add it with a new priority priority_queue = [(p, t) for p, t in priority_queue if t != 'task 2'] heapq.heapify(priority_queue) heapq.heappush(priority_queue, (4, ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - Hereโ€™s an example of how to use queue.PriorityQueue to implement a priority queue: from queue import PriorityQueue import threading, random, time # Create a PriorityQueue instance pq = PriorityQueue() # Define a worker function that will process tasks from the priority queue def worker(): ...
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python priority queue: a guide
Python Priority Queue: A Guide | Career Karma
December 1, 2023 - In our code, we first import the PriorityQueue class from the queue library, then we initialize a priority queue called ticket_holders. Next, we insert three tuples into our priority queue, which store the ticket numbers and names associated ...
๐ŸŒ
Squash
squash.io โ€บ python-priority-queue-a-practical-guide
Python Priority Queue Tutorial - Squash Labs
September 13, 2024 - Here's an example of how to implement a priority queue using the heapq module: import heapq class PriorityQueue: def __init__(self): self.heap = [] def push(self, item, priority): heapq.heappush(self.heap, (priority, item)) def pop(self): priority, ...
๐ŸŒ
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.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ python datatypes โ€บ python priority queue using queue, heapq and bisect modules
Python Priority Queue using queue, heapq and bisect Modules
March 6, 2024 - The following Python program uses the heapq module to implement a simple priority queue: import heapq class PriorityQueue: def __init__(self): self._queue = [] self._index = 0 def push(self, item, priority): heapq.heappush(self._queue, (-priority, ...
๐ŸŒ
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')
๐ŸŒ
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.
๐ŸŒ
dbader.org
dbader.org โ€บ blog โ€บ priority-queues-in-python
Priority Queues in Python โ€“ dbader.org
April 12, 2017 - Because heapq technically only provides a min-heap implementation, extra steps must be taken to ensure sort stability and other features typically expected from a โ€œpracticalโ€ priority queue. import heapq q = [] heapq.heappush(q, (2, 'code')) ...
๐ŸŒ
Pierian Training
pieriantraining.com โ€บ home โ€บ python tutorial: creating a priority queue in python
Python Tutorial: Creating a Priority Queue in Python - Pierian Training
April 10, 2023 - In Python, heap is implemented as an array. ... import heapq queue = [] def enqueue(item, priority): heapq.heappush(queue, (priority, item)) def dequeue(): if not queue: return None return heapq.heappop(queue)[1]