you can use this method as a trick multiply by -1 to inverse the order :

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)

output:

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
Answer from Ghassen on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ heapq.html
heapq โ€” Heap queue algorithm
Raise KeyError if not found.' entry = entry_finder.pop(task) entry[-1] = REMOVED def pop_task(): 'Remove and return the lowest priority task. Raise KeyError if empty.' while pq: priority, count, task = heappop(pq) if task is not REMOVED: del entry_finder[task] return task raise KeyError('pop from an empty priority queue')
๐ŸŒ
Stackify
stackify.com โ€บ a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - Heapified list: [1, 3, 8, 5, 7] After pushing 2: [1, 3, 2, 5, 7, 8] Popped smallest element: 1 Heap after pop: [2, 3, 8, 5, 7] The heapq module is ideal for scenarios requiring fine-grained control over priority queue behavior. Itโ€™s lightweight and efficient but lacks the thread-safe features ...
Discussions

Python priority queue pops in descending order - Stack Overflow
from queue import PriorityQueue q = PriorityQueue() q.put((2, 'code')) q.put((1, 'eat')) q.put((3, 'sleep')) while not q.empty(): next_item = q.get() print(next_item) # Result: # (1, 'eat') # (2, 'code') # (3, 'sleep') What can i do so that it will pop in a reverse order? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I remove the top element from priorityQueue
I believe itโ€™s popped=queue.get() More on reddit.com
๐ŸŒ r/learnpython
6
1
December 22, 2020
python - Can I get an item from a PriorityQueue without removing it yet? - Stack Overflow
I want to get the next item in queue but I don't want to dequeue it. Is it possible in Python's queue.PriorityQueue? From the docs, I don't see how can it be done More on stackoverflow.com
๐ŸŒ stackoverflow.com
A generic priority queue for Python - Stack Overflow
The documentation has since been ... to use popitem. 2022-02-18T09:06:55.743Z+00:00 ... I ended up implementing a wrapper for heapq, adding a dict for maintaining the queue's elements unique. The result should be quite efficient for all operators: class PriorityQueueSet(object): """ ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - Items are pushed onto the queue and are popped from the queue when they are due to be processed. The pop operation typically removes the item from the queue. However, it is sometimes possible to peek at the entry located at the front of the ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
February 23, 2026 - If maxsize is less than or equal to zero, the queue size is infinite. The lowest valued entries are retrieved first (the lowest valued entry is the one that would be returned by min(entries)). A typical pattern for entries is a tuple in the form: (priority_number, data).
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i remove the top element from priorityqueue
r/learnpython on Reddit: How do I remove the top element from priorityQueue
December 22, 2020 -

I am implementing a priority queue using priorityQueue from queues. However, I canโ€™t seem to get the right method to delete the top element from this queue. So far I have tried delete, pop and remove. I get errors for them all saying priorityQueue has no attribute like remove, delete or pop.

Please help! This should be fairly simple but itโ€™s driving me crazy

Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - A priority queue stores (priority, item) pairs so the element with the highest priority (or lowest, for min-heap) is removed first. Python ships two ready-made solutions: heapq and queue.PriorityQueue.
๐ŸŒ
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 data structure.
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - The last tiny detail to keep in ... the popped value index during a dequeue operation. Because elements are tuples with three components now, you ought to return the value located at index two instead of one. However, itโ€™d be safer to use the negative one as an index to indicate the last component of the tuple, regardless of its length. Your priority queue is almost ...
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ priority-queues-in-python
Introduction to Priority Queues in Python | Built In
A priority queue in Python is an abstract data structure (a data structure defined by its behavior) that is like a normal queue but where each item has a special โ€œkeyโ€ to quantify its priority. For example, if the movie theater decides to serve loyal customers first, it will order them by their loyalty, either by loyalty points or number of tickets purchased.
๐ŸŒ
dbader.org
dbader.org โ€บ blog โ€บ priority-queues-in-python
Priority Queues in Python โ€“ dbader.org
April 12, 2017 - Read on and find out what the Python standard library has to offer. 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.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python queue.priorityqueue methods
Python queue.priorityqueue Methods - Spark By {Examples}
May 31, 2024 - Popping elements from the priority queue using get() method ยท Check whether the priority queue is empty or not using empty() Following are quick examples of using priorityqueue methods in python.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ python-cookbook โ€บ 0596001673 โ€บ ch17s16.html
Modeling a Priority Queue - Python Cookbook [Book]
July 19, 2002 - The bisect module, from the standard ... """ bisect.insort(self.queue, (priority, data)) def pop(self): """ Pop the highest-priority element of the queue....
Authors ย  Alex MartelliDavid Ascher
Published ย  2002
Pages ย  608
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ heap-and-priority-queue-using-heapq-module-in-python
Heap and Priority Queue using heapq module in Python - GeeksforGeeks
July 23, 2025 - The basic operations associated with these priority queues are listed below: ... insert : To insert an element along with its priority. The element will be placed in the order of its priority only. pop : To pop the element with the highest priority.
๐ŸŒ
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 - # To create a max-heap, # just make every item the opposite sign when you push them into the heap #======================== # Test 1: heapify a list #======================== a = [3, 5, 1, 2, 6, 8, 7] heapq.heapify(a) # this turns the list to a priority queue print("a =", a) # note the difference between a and a after heapify() #======================== # Test 2: initialize with an empty list #======================== p_queue = [] # note that you will get an error if you do p_queue = heapq() # use heappush() heapq.heappush(p_queue, 10) heapq.heappush(p_queue, 5) heapq.heappush(p_queue, 20) print(p_queue) # Regardless of the order elements are pushed into heapq, # it always pops the smallest element.
๐ŸŒ
Brilliant
brilliant.org โ€บ wiki โ€บ priority-queues
Priority Queues | Brilliant Math & Science Wiki
So even though we put John in the queue first, we put Sally in with a higher priority, so she was processed first by the pop() function.
๐ŸŒ
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 typical Queue, the first element to enter is the first to leave (FIFO โ€“ First In First Out). 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.