If a is a PriorityQueue object, You can use a.queue[0] to get the next item:

from queue import PriorityQueue

a = PriorityQueue()

a.put((10, "a"))
a.put((4, "b"))
a.put((3,"c"))

print(a.queue[0])
print(a.queue)
print(a.get())
print(a.queue)
print(a.get())
print(a.queue)

output is :

(3, 'c')
[(3, 'c'), (10, 'a'), (4, 'b')]
(3, 'c')
[(4, 'b'), (10, 'a')]
(4, 'b')
[(10, 'a')]

but be careful about multi thread access.

Answer from HYRY on Stack Overflow
🌐
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.
🌐
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.
Discussions

python - Can I get an item from a PriorityQueue without removing it yet? - Stack Overflow
When you get item form the queue as per theory it will remove from the queue. You have to write your own function which will give you last element of PriorityQueue. You can create a peek function by inherit the priorityqueue. ... Suppose I extend PriorityQueue, I still need to access the underlying data store to implement peak right? But how? 2012-02-15T05:06:42.78Z+00:00 ... If you can check the code hg.python... More on stackoverflow.com
🌐 stackoverflow.com
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
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
🌐
Built In
builtin.com › data-science › priority-queues-in-python
Introduction to Priority Queues in Python | Built In
I hope this article helps you get ... queue can be implemented in Python by using a list, importing the heapq module or importing the queue module and using the PriorityQueue class....
🌐
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.
🌐
Linode
linode.com › docs › guides › python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - By default, a get request blocks waiting forever 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. 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.
🌐
Python Guides
pythonguides.com › priority-queue-in-python
Priority Queue in Python
December 12, 2025 - ... A priority queue is a special type of queue where each element is associated with a priority. 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.
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 ...
🌐
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.
🌐
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')
🌐
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: ... The get command dequeues the highest priority elements from the queue.
🌐
Towards Data Science
towardsdatascience.com › home › latest › priority queues in python
Priority Queues in Python | Towards Data Science
January 26, 2025 - It is worth familiarizing ourselves with the python ‘heapq’ module before we build our ‘PriorityQueue’ class. To start let’s import the ‘heapq’ module: ... If we wanted to get the N lowest or highest prices we can use the ‘.nsmallest()’ and ‘.nlargest()’ methods respectively.
🌐
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 ... the first argument is the priority and the second argument is the task itself. The get method retrieves the task with the highest priority from the queue....
🌐
Career Karma
careerkarma.com › blog › python › python priority queue: a guide
Python Priority Queue: A Guide | Career Karma
December 1, 2023 - The queue.PriorityQueue class creates a Python priority queue. This class is part of the Python queue library. You need to import the queue library to use this class. To retrieve an item from a PriorityQueue, you can use the get() method.
🌐
Bogotobogo
bogotobogo.com › python › python_PriorityQueue_heapq_Data_Structure.php
Python Tutorial: Data Structure - Priority Queue & heapq - 2020
__cmp__(self, other): return cmp(self.priority, other.priority) q = Q.PriorityQueue() q.put(Skill(5, 'Proficient')) q.put(Skill(10, 'Expert')) q.put(Skill(1, 'Novice')) while not q.empty(): next_level = q.get() print 'Processing level:', next_level.description ... New Level: Proficient New Level: Expert New Level: Novice Processing level: Novice Processing level: Proficient Processing level: Expert ... The heapq implements a min-heap sort algorithm suitable for use with Python's lists. This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
🌐
Real Python
realpython.com › queue-in-python
Python Stacks, Queues, and Priority Queues in Practice – Real Python
December 1, 2023 - Python has the heapq module, which conveniently provides a few functions that can turn a regular list into a heap and manipulate it efficiently. The two functions that’ll help you build a priority queue are:
🌐
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.
🌐
dbader.org
dbader.org › blog › priority-queues-in-python
Priority Queues in Python – dbader.org
April 12, 2017 - But in my mind queue.PriorityQueue is a good default choice. Sure, it might incur some unnecessary locking overhead—but it has a nice object-oriented interface and a name that states its intent clearly. Read the full “Fundamental Data Structures in Python” article series here.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › queue in python
Mastering Queue Data Structures in Python: Comprehensive Guide
October 23, 2023 - This is an example of priority queues using the queue.PriorityQueue class. Python provides a built-in implementation of the priority queue data structure.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
iO Flood
ioflood.com › blog › python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - The get(), put(), and empty() methods are used to interact with the underlying priority queue. ... Even though ‘Mid-priority item 1’ and ‘Mid-priority item 2’ have the same priority, they maintain the order in which they were inserted, ...