runcode runcode - 4 months ago 49
Python Question

How to check queue length in Python

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?

K Z K Z
Answer

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.