Search code examples
pythonpython-3.xpython-2.7data-structures

How to check deque length in Python


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?

Solution

  • 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.