Issue
get() removes and returns an item from Queue in Python.
import queue
q = queue.Queue() # Here
q.put("Apple")
q.put("Orange")
q.put("Banana")
print(q.get())
print(q.get())
print(q.get())
Output:
Apple
Orange
Banana
Now, I want to get the items in Queue without removing the items.
Is it possible to do this?
Solution
The
Queue
module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads.
As you can see, the Queue
module was created specifically for use with threads, providing only FIFO, LIFO and priority queues, none of which provide this functionality. However by examining the source code of the Queue
module, you can see that it simply uses a collections.deque
(double ended queue) which can easily accomplish your task. You may index the first item ([0]
) and .popleft()
in constant time.
Answered By - jamylak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.