Deques are a great way to handle memory efficient appends to a list-like object, it is a special module that allows you to handle list items in a more appropriate way.
To create a list simply import the
deque module from
collections library and call
deque(_items_) on a variable.
from collections import deque
Or if you wish to create an empty
dq = deque()
What happens if you want to create a deque of integers?
123)dq = deque(
You simply can’t. Why is that? Because Integers are not iterable in python but String are.
This is because Integers, unlike strings, don’t have a
__iter__ method and therefore they don’t return iterables.
We can access items in a deque with an index number.
You can convert an item to a item simply by wrapping them around an
0])one = int(dq[
We can append new items to our deque, to either left side or right side.
We can also add multiple values at once.
We can delete items in both sides.
Or rotate our items if we want.
We cannot slice our deques, at least not directly.
You can import
itertools and return a sliced list (not a deque) of items with
You can find more information about deques in the official Python documentation: Deque Objects