Deque in Python is a Doubly Ended Queue that belongs to the Collection module. It is generally a data structure like stack or queue, except that it offers quicker append and pop operations from both ends. Deque provides memory efficient and thread-supportive data insertion (append) and removal (pop) from both sides, with the performance as optimized as O(1).
The list object holds the capability to perform similar operations on fixed-length data with all the optimism and speed. However, contrary to deque, lists have a complexity of O(n) for append and pop operations.
Deque can be created of fixed length as well as arbitrary length. If, during the initialization of a deque object you are specifying a value for its maxlen parameter, then the deque items will not exceed the specified length. In case of excessive items, a subsequent amount of equal items will be removed from the other end of the object to make room for the recent ones. However, if one has not initially assigned a value for the maxlen parameter or have assigned None to it, then the deque can be extended as long as one wants.
Table of Contents
Syntax
class collections.deque([iterable [, maxlen]])
Deque objects support various methods to perform different operations on the passed iterable. Let’s now discuss them along with code based examples.
append(x)
The append(x) method will add a value on the right side of the collection which is passed in the argument of deque object.
Code Snippet
#importing deque from collections module
from collections import deque
#initializing the deque object
deq = deque(['apple', 'mango', 'orange'])
#printing the initial deque object
print("Printing the initial deque object items \n" , deq, '\n')
#append(x) demonstration
print("Appending a new element to the right of deque")
deq.append('papaya')
print(deq , '\n')
Output

appendleft(x)
This method will add a value on the left side of the collection which is passed in the argument of deque object.
Code Snippet
#appendleft(x) demonstration
print("Appending a new element to the left of deque")
deq.appendleft('strawberry')
print(deq , '\n')
Output

count(x)
This function will take an argument and check its number of occurrences within the deque. It is released in Python version 3.2.
Code Snippet
#count(x) demonstration
print("Counting the the occurrence of apple in deque")
print(deq.count('apple') , '\n')
Output

extend (iterable)
This function will add multiple values on the right side of the deque, because it takes an entire iterable in the argument.
Code Snippet
#extend(iterable) demonstration
deq.extend(['grapes', 'watermelon'])
print("Extending the deque with new items on the right")
print(deq, "\n")
Output

extendleft (iterable)
This function will add multiple values on the left side of the deque, because it takes an entire iterable in the argument. The order of the deque object will be reversed because of the left append.
Code Snippet
extendleft(iterable) demonstration
deq.extendleft(['pear', 'guava'])
print("Extending the deque with new items on the left")
print(deq, "\n")
Output

index(x [, start [, stop]])
The index(x) method will take an argument and return the index-position of the specified value. It will return the index of first occurrence of argument, however, if you are specifying the start and/or stop positions, then the searching will be performed within that bound.
If no match is found for the argument in the deque, a ValueError will be raised. This method is new in version 3.5.
Code Snippet
#index(x [, start [, stop]]) demonstration
print("Finding the index of an item")
print(deq.index('strawberry'), "\n")
Output

insert (i, x)
The insert(i, x) will insert the value x at the position index i. If the insertion will cause a deque to grow beyond the specified maxlen attribute, it will raise an IndexError. It was released in version 3.5.
Code Snippet
#insert(i,x) demonstration
print("Inserting an item to the deque by specifiying the index")
deq.insert(2,'banana')
print(deq, "\n")
Output

pop()
This method is used to remove the element from the right of the deque and return it. If the deque is empty, it will raise an IndexError.
Code Snippet
#pop() demonstration
print("Popping out last item from the right")
print(deq.pop(), "\n")
Output

popleft()
This function is used to remove the element from the left of the deque and return it. If the deque is empty, it will raise an IndexError.
Code Snippet
#popleft() demonstration
print("Popping out first item from the left")
print(deq.popleft(), "\n")
Output

remove(value)
The remove(x) function will remove the first occurrence of an element x from the deque. If not found, it will throw a ValueError.
Code Snippet
#remove() demonstration
print("Removing an item from the deque")
deq.remove("apple")
print(deq, "\n")
Output

reverse()
This method is new in version 3.2. It is used to reverse the order of the deque and then it returns None.
Code Snippet
#reverse() demonstration
print("Reversing the order of the deque")
print("Before Reversing: \n", deq, "\n")
deq.reverse()
print("After Reversing: \n",deq, "\n")
Output

rotate(n=1)
The rotate(n) function will rotate the deque by n steps. If n is positive, the deque will be rotated towards right, however, if n is negative the deque will be rotated towards left.
Rotating deque to one step towards right (i.e., n=1) when the deque is not empty is equivalent to dequeObj.appendleft(dequeObj.pop()).
Code Snippet
#rotate() demonstration
print("Rotating the deque by one item towards right")
deq.rotate(1)
print(deq, "\n")
print("Rotating the deque by one item towards left")
deq.rotate(-2)
print(deq, "\n")
Output

copy()
This method creates a copy of the deque.
Code Snippet
#copy() demonstration
copieddeque = deq.copy()
print("Copied deque: \n ", copieddeque)
Output

clear()
clear() will remove all the elements from the deque object, reverting its length back to zero.
Code Snippet
#clear demonstration
deq.clear()
print("After clearing the deque: \n", deq)
Output

maxlen
maxlen is an attribute of deque which is used to make a deque object bounded by specifying its maximum size, i.e., a limited or fixed-length deque. If it’s left unassigned or is assigned to None the deque will be unbounded and can grow infinitely.
Code Snippet
#maxlen demonstration
deq2 = deque([1,2,3,4,5], maxlen=10)
print("Printing initial deque:", deq2)
deq2.extend([6,7,8,9,10,11])
print("Printing the deque after extension \n", deq2)
print("1 has been removed since we tried to push items more than the length of 10 \n")
Output

Conclusion
In this well explained article we discussed Python Deque a Double-Ended Queue and its implementation. We also observed the working mechanisms of its different functions with the help of coding based examples as well as their conceptual explanation. If you are a beginner trying to learn Python and its modules, this article can be a great help to you for understanding the Deque data structure from Python’s Collections module.