How to check deque length in Python
Asked Answered
M

4

78

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?
Mound answered 22/9, 2012 at 23:22 Comment(4)
Did you try len(queue)? That's usually how Python handles element counts.Poverty
print(len(my_queue.queue)) worked for me.Odo
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...)Delmadelmar
For computing the length of a queue.Queue (or multiprocessing.Queue) object, refer to Get length of Queue in Python's multiprocessing library - Stack OverflowHonourable
M
86

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.

Merthiolate answered 22/9, 2012 at 23:24 Comment(5)
AttributeError: Queue instance has no attribute 'len' I used qsize() instead docs.python.org/2.7/library/queue.htmlPrune
@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.Stylographic
This answer is wrong. There is no such attribute at all.Rollie
@Rollie Are you sure? It definitely does exist for a collections.deque which is what the question is asking. Per the documentation: 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).Advancement
@SmallChess, make sure it's queue= deque([]) rather than queue= deque()Augustina
G
65

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.

Growing answered 27/9, 2016 at 6:24 Comment(2)
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.Advancement
@Advancement But it brought googlers like me to the answer they did expect! Not sure why the queue.Queue class has no regular length…Turmoil
A
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...

Adrianeadrianna answered 22/2, 2019 at 12:37 Comment(0)
O
0

If you run into a problem where the error is

"AttributeError: collections.deque has no attribute length"

Then the reason is that you probably used JavaScript syntax in Python, i.e. you tried to call

from collections import deque
q = deque()
q.append(2)
q.length # This is wrong

The correct way to get the length of a deque is just the Pythonic len() function

from collections import deque
q = deque()
q.append(2)
print(len(q))
Oppen answered 19/1 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.