You can get the items in a queue without removing the items as shown below:
import queue
q = queue.Queue()
q.put("Apple")
q.put("Orange")
q.put("Banana")
print(q.queue[0]) # Here
print(q.queue[1]) # Here
print(q.queue[2]) # Here
print(q.queue) # Here
Output:
Apple
Orange
Banana
deque(['Apple', 'Orange', 'Banana'])
And, you can also change the items in a queue as shown below:
import queue
q = queue.Queue()
q.put("Apple")
q.put("Orange")
q.put("Banana")
q.queue[0] = "Strawberry" # Here
q.queue[1] = "Lemon" # Here
q.queue[2] = "kiwi" # Here
print(q.queue[0])
print(q.queue[1])
print(q.queue[2])
print(q.queue)
Output:
Strawberry
Lemon
kiwi
deque(['Strawberry', 'Lemon', 'kiwi'])
But, you cannot add items to a queue without put() as shown below:
import queue
q = queue.Queue()
q.queue[0] = "Apple" # Cannot add
q.queue[1] = "Orange" # Cannot add
q.queue[2] = "Banana" # Cannot add
print(q.queue[0])
print(q.queue[1])
print(q.queue[2])
print(q.queue)
Then, the error below occurs:
IndexError: deque index out of range
collections.deque
instead, are you using threads? – Appolonia