runcode runcode - 8 months ago 112
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....

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?


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.