What is deque in Python?


Are you a Python Programmer and looking to add queues and stack data structure functionalities in a program?

In the collections module, the deque is a collection that works as a double-ended queue. Unlike a normal queue, where insertion happens on one side and removal on another. In a deque, insertion, and removal can take place on either side. This makes it to use as a stack or queue when requires.

In this tutorial, we will explain each function of deque one by one with examples.

How to create a basic deque in Python?

The following example creates a very basic deque by using the collections module.

 #Importing deque from collection module
 from collections import deque 
 myQueue = deque(['34','56','78']);  
 print(myQueue);

Output->

deque(['34', '56', '78'])

What are the basic operations in deque?

Following is the list of basic operations in tabular form with a description.

Operation Description
append(value) Inserts a value on the right end of an already created deque.
pop() This function removes and returns the rightmost element from the deque.
appendleft(value)Insert the given value on the left end of the deque.
popleft()Similar to the pop(), but applies on the left end.

Example program with basic operations:-

#Importing deque from collection module
 from collections import deque 
 myQueue = deque(['34','56','78']);  
 print(myQueue);
 myQueue.append('7');
 print("Deque after append(7) \n",myQueue);
 myQueue.appendleft('9');
 print("Deque after appendleft(9) \n",myQueue);
 value = myQueue.pop();
 print("poped value with pop()= ", value, "Deque after pop() \n",myQueue);
 value = myQueue.popleft();
 print("poped with popleft() value = ", value, "Deque after pop() \n",myQueue);

Output->

deque(['34', '56', '78'])
 Deque after append(7)
  deque(['34', '56', '78', '7'])
 Deque after appendleft(9)
  deque(['9', '34', '56', '78', '7'])
 poped value with pop()=  7 Deque after pop()
  deque(['9', '34', '56', '78'])
 poped with popleft() value =  9 Deque after pop()
  deque(['34', '56', '78'])

More useful operations-

Operations Descriptions
extend(itobj)The function adds all values of a given iterable object at the right side of the deque.
extendleft(itobj)Add iterable on the left side.
#Importing deque from collection module
 from collections import deque 
 myQueue = deque(['34','56','78']);  
 print(myQueue);
 list = [4,7,12];
 myQueue.extend(list);
 print("after extend list = ",list , (myQueue));
 #extend on left side
 leftIt = ['a','b'];
 myQueue.extendleft(leftIt);
 print("after extendleft list = ",leftIt , (myQueue));
 print(myQueue);

Output->

deque(['34', '56', '78'])
 after extend list =  [4, 7, 12] deque(['34', '56', '78', 4, 7, 12])
 after extendleft list =  ['a', 'b'] deque(['b', 'a', '34', '56', '78', 4, 7, 12])
 deque(['b', 'a', '34', '56', '78', 4, 7, 12])

rotate(intValue) – Rotates either left or right by the integer value given. If the value is positive rotation is towards the right, if negative rotation is left and on zero no rotation.

#Importing deque from collection module
 from collections import deque 
 myDeque = deque(['34','56','78','a','8','b']);  
 print(myDeque);
 myDeque.rotate(2);
 print("After Rotating with value 2 :",  myDeque);
 myDeque.rotate(-1);
 print("After Rotating with value -1 :",  myDeque);
 print(myDeque);
 myDeque.rotate(0);
 print("After Rotating with value 0 :",  myDeque);
 print(myDeque);

Output->

deque(['34', '56', '78', 'a', '8', 'b'])
 After Rotating with value 2 : deque(['8', 'b', '34', '56', '78', 'a'])
 After Rotating with value -1 : deque(['b', '34', '56', '78', 'a', '8'])
 deque(['b', '34', '56', '78', 'a', '8'])
 After Rotating with value 0 : deque(['b', '34', '56', '78', 'a', '8'])
 deque(['b', '34', '56', '78', 'a', '8'])

remove(value) – Removes the given value from the list. If not present, no change in the list. If present multiple times the function removes the first occurrence.

 #Importing deque from collection module
 from collections import deque 
 myDeque = deque(['34','56','78','7','a','8','b','7']);  
 print(myDeque);
 myDeque.remove('78');
 print("After Call remove(78)",myDeque);
 myDeque.remove('7');
 print("After Call remove(7)",myDeque);

Output->

 deque(['34', '56', '78', '7', 'a', '8', 'b', '7'])
 After Call remove(78) deque(['34', '56', '7', 'a', '8', 'b', '7'])
 After Call remove(7) deque(['34', '56', 'a', '8', 'b', '7'])

reverse() – This function reverses the list. The last element becomes the first and the first becomes the last element.

#Importing deque from collection module
 from collections import deque 
 myDeque = deque(['34','56','78','7','a','8','b','7']);  
 print(myDeque);
 myDeque.reverse();
 print("After reverse :", myDeque);

Output->

  deque(['34', '56', '78', '7', 'a', '8', 'b', '7'])
 After reverse : deque(['7', 'b', '8', 'a', '7', '78', '56', '34'])

insert(index,value) – The deque function inserts a given value at the given index. An index is an integer number that starts with zero. The first element has an index value of zero. It keeps increasing from left to right.

#Importing deque from collection module
 from collections import deque 
 myDeque = deque(['34','56','78','7','a','8','b','7']);  
 print(myDeque);
 myDeque.insert(4,50);
 print("After inserting 50 at index 4 :", myDeque);

Output->

 deque(['34', '56', '78', '7', 'a', '8', 'b', '7'])
 After inserting 50 at index 4 : deque(['34', '56', '78', '7', 50, 'a', '8', 'b', '7'])

Share the Article