## DEV Community

Abdur-Rahmaan Janhangeer

Posted on

python lists are nice, deque spice things up. let's see how.

disclaimer: that's a somewhat really complete guide on deque.

bonus: sample program at the end

## What does deque stands for?

deque stands for double-ended queue

let's import only deque as we are experimenting

``````from collections import deque
``````

next we create an instance of it

``````list1 = deque()
``````

that one is missed by many, many, (once again) many deque tutorials

``````list1 = deque(['a', 'b', 'c'])
``````

the fact that you can populate it on creation

much like a list, .append can be used

``````>>> list1.append('a')
>>> list1
deque(['a'])
``````

## Extending

you can also add by extending

``````>>> list1.extend('a')
>>> list1
deque(['a'])
>>> list1.extend('bc')
>>> list1
deque(['a', 'b', 'c'])
``````

you can also extend using list

``````>>> list1.extend(['def'])
>>> list1
deque(['a', 'b', 'c', 'def'])
>>> list1.extend(['g', 'h', 'i'])
>>> list1
deque(['a', 'b', 'c', 'def', 'g', 'h', 'i'])
``````

## From the left

``````>>> list1.extendleft([1, 2, 3])
>>> list1
deque([3, 2, 1, 'a', 'b', 'c', 'def', 'g', 'h', 'i'])
>>> list1.appendleft([4, 5, 6])
>>> list1
deque([[4, 5, 6], 3, 2, 1, 'a', 'b', 'c', 'def', 'g', 'h', 'i'])
``````

## Getting elements

works just like lists

``````>>> list1[0]
[4, 5, 6]
``````

## Getting index of element

⚠️ again just like lists but with py3.5 >=

``````>>> list1.index('a')
4
``````

## Getting index between range

let us say you want to search an element between the index 5 and 9
we want to search for 'b'

``````>>> deque(['a', 'b', 'c', 'd', 'r', 't', 'd', 'b', 's', 'p']).index('b', 5, 9)
7
``````

where searching is lower bound inclusive

## Pop joins the game

``````>>> list1.pop()
>>> list1
deque([[4, 5, 6], 3, 2, 1, 'a', 'b', 'c', 'def', 'g', 'h'])
``````

and from the left

``````>>> list1.popleft()
>>> list1
deque([3, 2, 1, 'a', 'b', 'c', 'def', 'g', 'h'])
``````

## A note on non-pep8 compliance

notice that popleft etc don't follow pep8

## A fixed-width list (great! no need to check and delete)

a deque can be constrained as follows

``````pipe = deque(maxlen=5)
``````

``````for i in range(0, 10+1):
pipe.append(i)
print(pipe)
``````

gives out:

``````deque([6, 7, 8, 9, 10], maxlen=5)
``````

we see that 0 to 5 has been automaitcally eliminated

## Roation is amazing

let us say you have

``````>>> alphs = deque()
>>> alphs.extends('abcdef')
>>> alphs.rotate(2)
>>> alphs
deque(['e', 'f', 'a', 'b', 'c', 'd'])
``````

rotating by a negative index e.g. -2 rotates in the opposite direction

## Implementing a simple cipher

using rotate, a cipher is very easy

``````from collections import deque
import string

original = deque()
rotated = deque()

original.extend(string.ascii_lowercase) # a .. z
rotated.extend(string.ascii_lowercase)
rotated.rotate(2)

print(original)
print(rotated)

to_encode = 'myhouse'
encoded_text = ''
for c in to_encode:
index_char = original.index(c)
encoded_char = rotated[index_char]
encoded_text += encoded_char

print(encoded_text)

decoded_text = ''
for c in encoded_text:
index_char = rotated.index(c)
decoded_char = original[index_char]
decoded_text += decoded_char

print(decoded_text)

``````

gives out

``````deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
deque(['y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'])
kwfmsqc
myhouse
``````

## A warning though

don't do

``````original = rotated = deque()
``````

as changing one also changes the other due to python references

as that snippet shows

``````>>> original = rotated = deque()
>>> rotated.extend('abc')
>>> rotated
deque(['a', 'b', 'c'])
>>> original
deque(['a', 'b', 'c'])
>>> original.extend('d')
>>> rotated
deque(['a', 'b', 'c', 'd'])
``````

## What we did not cover with examples

• .remove(4): removes the element 4 in the deque else raises ValueError
• .reverse(): reverses deque, [1, 2, 3] becomes [3, 2, 1]
• .clear(): clears deque, deque(['a', 'b', 'c']) becomes deque([])
• .count('a'): gives out the number of occurances. deque(['a', 'b', 'c']).count('a') gives out 1
• .insert(index, element). x = deque(['a', 'b', 'c']); x.insert(1, 't'); where x becomes deque(['a', 't', 'b', 'c'])

``````