Source

An intro to Deque module

Hi there folks. Recently I got to know about the Python deque module. It is a part of the collections library. It provides you with a double ended queue which means that you can append and delete elements from either side of the list. So without wasting any time lets begin. First of all you have to import the deque module from the collections library. This can be done by typing:

from collections import deque

Now we can instantiate a deque object.

d = deque()

That was simple. It works like python lists and provides you with somewhat similar methods as well. For example you can do:

d = deque()
d.append('1')
d.append('2')
d.append('3')
len(d)
d[0]
d[-1]

Output:

3
'1'
'3'

You can pop values from both sides of the deque. This means that you can do this:

d = deque('12345')
len(d)
d.popleft()
d.pop()
d

Output:

5 
'1' 
'5' 
deque(['2', '3', '4'])

We can also limit the amount of items a deque can hold. By doing this when we achieve the maximum limit of out deque it will simply pop out the items from the opposite end. It is better to explain it using an example so here you go:

d = deque(maxlen=30)

Now whenever you insert values after 30, the leftmost value will be popped from the list. You can also expand the list in any direction with new values:

d = deque([1,2,3,4,5])
d.extendleft([0])
d.extend([6,7,8])
d

Output:

deque([0, 1, 2, 3, 4, 5, 6, 7, 8])

So that’s it! That was a basic overview of the deque module in Python stdlib. I hope you learned something new today. I hope to see you guys later and don’t forget to follow this blog to get your daily dose of Python tips and tutorials. For further reading I would suggest the following links:

Newsletter

×

If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

ashu mishra

Nice Explanation

Yasoob
In reply to ashu mishra

Thanks! :) I am happy that you found it helpful.

Ryan

> Now whenever you insert values after 30, the leftmost value will be popped from the list.

This is correct, assuming you’re just “append”-ing. If you “appendleft”, the rightmost value will be dropped. Cool tip!

groovy

Good tutorial on a fundamental data structure dequeue

AS

Is there a way to insert an element into a deque in an arbitrary position? I mean, something analogous to the insert method in list?

Yasoob
In reply to AS

No. As far as I know you can not do it. However, if that is really what you want to do then you can adopt a somewhat hacky way of first converting it into a list, inserting the element in an arbitrary position and then casting it back into a deque. But I would not recommend that.

Yasoob
In reply to AS

davidxand

Hi there, thanks for your write-up on this matter. All your stuff is really helpful. Noob to python here.

I’m assuming that deques allow for string elements? Is there a way to force the container to accommodate only integers for example?

Secondly, if I wanted to insert double-digit numbers into the list. My attempt has been unsuccessful - e.g. how do I return the element ‘10’ in the list?

import collections
t=collections.deque('12345678910')
print(t)

deque(['1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '0'])

Finally, what’s the best way to sum the elements in the container? Would it be necessary to loop through the list and sum iteratively?

Yasoob
In reply to davidxand

Hi David!

I am assuming that when adding something to the deque you can add a check for whether the item being added is an integer or not. This is pretty easy by using the type() function.

In order to accommodate 10 as well, you can initialise the deque like this:

t=collections.deque([1,2,3,4,5,6,7,8,9,10])

As far as summing the elements go, I would personally prefer to use a list comprehension and the sum function like this:

total = sum([int(elem) for elem in t])

In this case I am assuming that the deque might have integers in string form. If you know for sure that the elements are all integers you can omit “int”.

I hope my answer helps you. If you still have any questions please let me know!

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊

OK