How to check a deque’s length in python?
I don’t see they provide deque.length 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 deque?
and can we initialize like
queue = deque([]) #is this length 0 deque?
len(queue)should give you the result, 3 in this case.Specifically,
len(object)function will callobject.__len__method [reference link]. And the object in this case isdeque, which implements__len__method (you can see it bydir(deque)).Yes it will be 0 for empty
deque.