Can I get an item from a PriorityQueue without removing it yet?
Solution 1
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.
Solution 2
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.
Solution 3
If you want next element in the PriorityQueue, in the order of the insertion of the elements, use:
for i in range(len(queue.queue)):
print queue.queue[i]
this will not pop anything out.
If you want it in the priority order, use:
for i in range(len(queue.queue)):
temp = queue.get()
queue.put(temp)
print temp
If you are using a tuple, instead of a single variable, replace temp by:
((temp1,temp2))
Solution 4
Assuming your items stored in the PriorityQueue is a tuple (priority, value),
def peek(pq):
return pq.queue[0][1]
Solution 5
Indexing the first element of the queue should work. If you're using the heapq
library, the document mentions:
The interesting property of a heap is that its smallest element is always the root,
heap[0]
.
Related videos on Youtube
Jiew Meng
Web Developer & Computer Science Student Tools of Trade: PHP, Symfony MVC, Doctrine ORM, HTML, CSS, jQuery/JS Looking at Python/Google App Engine, C#/WPF/Entity Framework I hope to develop usable web applications like Wunderlist, SpringPad in the future
Updated on October 20, 2021Comments
-
Jiew Meng over 2 years
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 -
Jiew Meng over 12 yearsSuppose I extend PriorityQueue, I still need to access the underlying data store to implement peak right? But how?
-
Nilesh over 12 yearsIf you can check the code hg.python.org/cpython/file/2.7/Lib/Queue.py then they use list for storing the data. So you can play with the list as you want which is
self.queue
in the example. Also u can check the_get
method of PriorityQueue so if you want to change that functionality then also override that function. -
Jiew Meng over 12 yearsis cpython the same as python?
-
Zitrax about 10 yearsAnd note that get() is blocking by default which indexing would not do.
-
Sush about 8 yearsIn the case of multi threading, we could lock the q.mutex, and release the lock after reading q.queue[0].
-
Rufus over 7 yearsIt appears that while
q.queue[0]
returns the highest priority item in the queue,q.queue[1]
does not necessarily return the 2nd highest priority item -
g-abello about 7 years@Woofas What you say is totally true and from my point of view, it's an unexpected behavior... Do you know why it happens?
-
Rufus about 7 years@g-abello I have no clue... I suppose it's doing some lazy evaluation to just keep the highest priority item on top without sorting the rest
-
MikeyE about 7 yearsThis solution isn't limited to just PriorityQueue objects. It also works for Queue objects. Seems like the most elegant solution to me. No offense intended, but I don't see how the other answers come close to this one (imho).
-
Marawan Okasha over 6 years@Woofas you will find that the 2nd highest priority is either
q.queue[1]
orq.queue[2]
. That is because according to the theory of Priority Queues, the parent (q.queue[0]
in this case) must have a higher priority that either of its two children (q.queue[1]
andq.queue[2]
), but the specific order of these two children is not important. This means that the whole q.queue is not absolutely sorted, only "heap" sorted (i.e every level has a higher priority than the level below it) -
Atnas almost 6 yearsThe first part does not give you the elements in the inserted order, but the first element will be the one with lowest value.
-
john ktejik over 5 yearswhy the example you have? It doesn't show what the OP is asking for
-
Sobir Bobiev about 3 yearsin terms of complexity, does
a.queue[0]
first construct the array and then return the first element? so O(n)? -
foki almost 3 years@SobirBobiev No. Queue uses
a.queue
as its storage. The list is how the queue is stored in memory, in plain. As long as the queue is constructed, the list must there. By accessing the list directly you just circumvent the whole logic of the queue and go straight to its storage. -
foki almost 3 years@SobirBobiev
a.queue[0]
is, thus, constant time. -
gebbissimo about 2 years"but be careful about multi thread access." -> Any idea how to solve multi thread access?
-
Stephen C about 2 years@gebbissimo With multithreaded access, you can't assume that the top item will stay the top item, because other threads may manipulate the PriorityQueue. See my answer for an example: stackoverflow.com/a/72277696/7228140