1

I am looking for a simple thing if an object exists in a Python Queue

eg

queue = Queue.Queue()
queue.put(1)
queue.put(2)
queue.put(3)
queue.put(4)

if 3 in queue:
     print "yes"

I am getting Error;

Traceback (most recent call last):

File "", line 1, in

TypeError: argument of type 'instance' is not iterable

I am new to Python but I know its easy to handle this in Java.

Queue<Integer> q = new LinkedList<>();
//here is the method
q.contains(obj)

I couldn't get any solution to handle this case and I need this in one of my Python problems. I would really appreciate, if some one could help.

-Krishna

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Queues are synchronized sequences optimized for fast access on the edges but not for random access (as you'd need to synchronize all of their contents all the time). You should not be using a queue if you don't need it - a simple `list` will perform much better for almost all non-threaded tasks. Also, `queue` is not an equivalent of Java's `LinkedList` - `collections.deque` is closer to that (it's a doubly-linked list) but there is no real native linked list in Python. – zwer Jun 29 '18 at 04:18
  • I answerred this here: https://stackoverflow.com/questions/1581895/how-check-if-a-task-is-already-in-python-queue/72375247#72375247 – r_agha_m May 25 '22 at 10:36

0 Answers0