What is deque in Python?


Are you a Python programmer who needs to incorporate queues and stacks into your program? Python provides a data structure in the collections module named deque.

Deque is a double-ended queue. In contrast to a regular queue, where insertion takes place on one end and removal occurs on the other, deque allows for insertion as well as removal on either end. This allows it to be used as a stack or queue as needed.

The following tutorial will provide a step-by-step explanation of each function of deque, together 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);
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, there is 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);
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, there is no change in the list. If current 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 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