73

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?
chris
  • 1,831
  • 18
  • 33
runcode
  • 3,533
  • 9
  • 35
  • 52
  • 4
    Did you try `len(queue)`? That's usually how Python handles element counts. – mjgpy3 Sep 22 '12 at 23:25
  • `print(len(my_queue.queue))` worked for me. – rustyMagnet Dec 04 '20 at 11:34
  • 1
    I hesitate to edit a question with this many upvotes, but I think there is a case for using a word other than "queue" in the question title and body, since many google searches about python `Queue` are going to end up here, and the accepted answer simply will not work for a `Queue`. (and yes, I know a `deque` is also a type of queue - it's just an unfortunate word in this case...) – Colin T Bowers May 12 '21 at 12:47
  • For computing the length of a `queue.Queue` (or `multiprocessing.Queue`) object, refer to [Get length of Queue in Python's multiprocessing library - Stack Overflow](https://stackoverflow.com/questions/41952413/get-length-of-queue-in-pythons-multiprocessing-library) – user202729 Aug 14 '21 at 03:15

4 Answers4

80

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.

K Z
  • 29,661
  • 8
  • 73
  • 78
  • 14
    AttributeError: Queue instance has no attribute '__len__' I used qsize() instead https://docs.python.org/2.7/library/queue.html – otgw Nov 20 '15 at 19:56
  • 8
    @memo: read the question body. `collections.deque` is different from `queue.Queue`. The latter is expected to be used in a multithreading case where the size may be changed in another thread. – jfs Dec 06 '15 at 10:14
  • 4
    This answer is wrong. There is no such attribute at all. – ABCD Mar 30 '18 at 12:40
  • 2
    @SmallChess Are you sure? It definitely does exist for a `collections.deque` which is what the question is asking. Per the documentation: https://docs.python.org/3/library/collections.html#deque-objects towards the end of the section... *"In addition to the above, deques support iteration, pickling, **len(d),**..."* (emphasis mine). – rayryeng Apr 19 '18 at 18:46
  • 1
    @SmallChess, make sure it's queue= deque([]) rather than queue= deque() – Jiaji Li Nov 17 '19 at 16:41
63

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.

Mani
  • 5,401
  • 1
  • 30
  • 51
  • 10
    Please note that this is for the `Queue` class, which is not the same as the one from `collections.deque`, which is what the OP is actually asking for. – rayryeng May 03 '18 at 07:03
  • @rayryeng But it brought googlers like me to the answer they _did_ expect! Not sure why the `queue.Queue` class has no regular length… – gerrit Apr 28 '21 at 17:06
1

Yes we can check the length of queue object created from collections.

from collections import deque
class Queue():
    def __init__(self,batchSize=32):
        #self.batchSie = batchSize
        self._queue = deque(maxlen=batchSize)

    def enqueue(self, items):
        ''' Appending the items to the queue'''
        self._queue.append(items)

    def dequeue(self):
        '''remoe the items from the top if the queue becomes full '''
        return self._queue.popleft()

Creating an object of class

q = Queue(batchSize=64)
q.enqueue([1,2])
q.enqueue([2,3])
q.enqueue([1,4])
q.enqueue([1,22])

Now retrieving the length of the queue

#check the len of queue
print(len(q._queue)) 
#you can print the content of the queue
print(q._queue)
#Can check the content of the queue
print(q.dequeue())
#Check the length of retrieved item 
print(len(q.dequeue()))

check the results in attached screen shot

enter image description here

Hope this helps...

Vaibhav K
  • 2,762
  • 3
  • 21
  • 22
-7

Use queue.rear+1 to get the length of the queue

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 1
    No it won't. There's no `rear` attribute in any queue seen in the Python language, at least not within the core stack. – rayryeng Apr 19 '18 at 23:45
  • This is `collections.deque` from python, not your circular queue homework from college. – nurettin Mar 14 '22 at 07:46