How to check queue length in Python

runcode picture runcode · Sep 23, 2012 · Viewed 97.1k times · Source

How to check Queue's length in python?

I dont see they provide Queue.lenght in python....

http://docs.python.org/tutorial/datastructures.html

from collections import deque
queue = deque(["Eric", "John", "Michael"])

how to check the length of this queue?

and can we initialize like

queue= deque([])   #is this length 0 queue?

Answer

K Z picture K Z · Sep 23, 2012

len(queue) should give you the result, 3 in this case.

Specifically, len(object) function will call object.__len__ method [reference link]. And the object in this case is deque, which implements __len__ method (you can see it by dir(deque)).


queue= deque([])   #is this length 0 queue?

Yes it will be 0 for empty deque.