Iterate over deque in python
I used a while
loop for deque
iteration, as follows:
from collections import deque
lst = [1,2,4,8]
queue=deque(lst)
print(queue)
while queue:
print('{}{}'.format("head: ",queue[0]))
queue.popleft()
output:
deque([1, 2, 4, 8])
head: 1
head: 2
head: 4
head: 8
You can directly iterate over the deque.
for i in d:
doSomethingWith(i)
(see the examples in the documentation: https://docs.python.org/2/library/collections.html#collections.deque)