Add Superpowers To Your Python Lists Using This Feature
Tags: python
Published: 2019-03-07 08:52:58

---
title: Add Superpowers To Your Python Lists Using This Feature
published: true
description: a smile appears on py devs' faces
tags: python
cover_image: https://images.unsplash.com/photo-1495333003700-eb75daae834b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=80
---

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 **d**ouble-ended **queue**

let's import only deque as we are experimenting

```python
from collections import deque
```

next we create an instance of it

```python
list1 = deque()
```
## Adding elements on creation

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

```python
list1 = deque(['a', 'b', 'c'])
```
the fact that you can populate it on creation

## Adding elements

much like a list, .append can be used

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

## Extending

you can also add by extending

```python
>>> list1.extend('a')
>>> list1
deque(['a'])
>>> list1.extend('bc')
>>> list1
deque(['a', 'b', 'c'])
```
you can also extend using list

```python
>>> 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

```python
>>> 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

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

## Getting index of element

:warning: again just like lists but with **py3.5 >=**

```python
>>> 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'
```python
>>> 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

```python
>>> list1.pop()
>>> list1
deque([[4, 5, 6], 3, 2, 1, 'a', 'b', 'c', 'def', 'g', 'h'])
```
and from the left
```python
>>> 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

```python
pipe = deque(maxlen=5)
```
now let us add elements

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

```python
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

```python
>>> 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

```python
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

```python
original = rotated = deque()
```
as changing one also changes the other due to python references

as that snippet shows

```python
>>> 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'])