Deque in Python
Deque in Python
Deque (Doubly Ended Queue) in Python is implemented using the module “collections“.
Deque is preferred over a list in the cases where we need quicker append and pop operations
from both the ends of the container, as deque provides an O(1) time complexity for append and
pop operations as compared to a list that provides O(n) time complexity.
# Declaring deque
queue = deque(['name','age','DOB'])
print(queue)
Output
deque(['name', 'age', 'DOB'])
Operations on deque
Example 1: Appending Items Efficiently
append():- This function is used to insert the value in its argument to the right end of the
deque.
appendleft():- This function is used to insert the value in its argument to the left end of the
deque.
Python3
import collections
# initializing deque
de = collections.deque([1, 2, 3])
de.append(4)
print(de)
de.appendleft(6)
print(de)
Output
deque: deque([1, 2, 3])
pop():- This function is used to delete an argument from the right end of the deque.
popleft():- This function is used to delete an argument from the left end of the deque.
Python3
import collections
# initializing deque
de = collections.deque([6, 1, 2, 3, 4])
de.pop()
print(de)
de.popleft()
print(de)
Output
deque: deque([6, 1, 2, 3, 4])
index(ele, beg, end):- This function returns the first index of the value mentioned in
arguments, starting searching from beg till end index.
insert(i, a) :- This function inserts the value mentioned in arguments(a) at index(i) specified
in arguments.
remove():- This function removes the first occurrence of the value mentioned in arguments.
count():- This function counts the number of occurrences of value mentioned in arguments.
Python3
import collections
# initializing deque
de = collections.deque([1, 2, 3, 3, 4, 2, 4])
print (de.index(4,2,5))
de.insert(4,3)
print (de)
print (de.count(3))
de.remove(3)
print (de)
Output
The number 4 first occurs at a position :
4
The deque after inserting 3 at 5th position is :
deque([1, 2, 3, 3, 3, 4, 2, 4])
The count of 3 in deque is :
3
The deque after deleting first occurrence of 3 is :
deque([1, 2, 3, 3, 4, 2, 4])
# initializing deque
de = deque([1, 2, 3, 4, 5, 6])
de.pop()
print("\nThe deque after deleting from right is: ", end = '')
print(de)
Output
Current Deque: deque([1, 2, 3, 4, 5, 6])
Size of Deque: 6
Deque[0] :- We can access the front element of the deque using indexing with de[0].
Deque[-1] :- We can access the back element of the deque using indexing with de[-1].
Python3
# initializing deque
de = deque([1, 2, 3, 4, 5, 6])
Output
Current Deque: deque([1, 2, 3, 4, 5, 6])
Front element of the deque: 1
Back element of the deque: 6
import collections
# initializing deque
de = collections.deque([1, 2, 3,])
de.extend([4,5,6])
print (de)
de.extendleft([7,8,9])
# rotates by 3 to left
de.rotate(-3)
print (de)
de.reverse()
print (de)
Output
The deque after extending deque at end is :
deque([1, 2, 3, 4, 5, 6])
The deque after extending deque at beginning is :
deque([9, 8, 7, 1, 2, 3, 4, 5, 6])
The deque after rotating deque is :
deque([1, 2, 3, 4, 5, 6, 9, 8, 7])
The deque after reversing deque is :
deque([7, 8, 9, 6, 5, 4, 3, 2, 1])