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?
queue::size() is used to check whether the size of the associated queue container. This function returns an unsigned int value, i.e the size of the queue container, or the number of elements present in a queue container. This function returns 0 if the queue is empty or having no elements in it.
A double-ended queue, or deque, has the feature of adding and removing elements from either end. The Deque module is a part of collections library. It has the methods for adding and removing elements which can be invoked directly with arguments.
Of course, it can be done using lists, but with deques, you already have the interface for that, and it's much faster.
The deque data structure from the collections module does not have a peek method, but similar results can be achieved by fetching the elements with square brackets.
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
.
it is simple just use .qsize() example:
a=Queue() a.put("abcdef") print a.qsize() #prints 1 which is the size of queue
The above snippet applies for Queue()
class of python. Thanks @rayryeng for the update.
for deque from collections
we can use len()
as stated here by K Z.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With