Python collections Deque() Method with Examples

Deque:

In Python, the module “collections” is used to implement a Deque (Double Ended Queue). Deque is chosen over list when we need faster append and pop operations from both ends of the container, as deque has an O(1) time complexity for append and pop operations, whereas list has an O(n) time complexity.

Access Operations on Deque():

append():

append() function adds the value in its argument to the right end of the 
deque.

appendleft():

 appendleft() function inserts the value in its argument to the left end of
 the deque.

pop():

 pop() function is used to remove an argument from the deque's right end.

popleft():

 popleft() function is used to remove an argument from the deque's left end.

index(ele, beg, end):

 This method returns the first index of the value specified in parameters,
 beginning with beg and ending with end index.

insert(i, a):

Inserts the value specified in arguments(a) at the index(i) specified in
arguments.

remove():

This function deletes the first occurrence of the value specified in the 
parameters.

count():

This function counts the number of times the value specified in arguments 
appears.

extend(iterable):

This function adds several values to the right end of a deque. 
The passed argument is iterable.

extendleft(iterable):

This function is used to add numerous values to the deque's left end. 
The passed argument is iterable. As a result of left appends, the order is 
reversed.

reverse():

 This function reverses the order of deque elements.

rotate():

This function rotates the deque by the number of arguments supplied. 
If the provided integer is negative, the rotation is to the left. Otherwise, 
rotate to the right.

collections Deque() Method with Examples in Python

1)append(), appendleft(), pop() ,popleft() Operations on deque

Approach:

  • Import collections module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the deque() function for initializing the deque and store it in another variable.
  • Append some random element to the deque using the append() function to insert an element at the right end of a deque.
  • Print the deque after appending.
  • Append left some random element to the deque using the appendleft() function to insert an element at the left end of a deque. It inserts an element at the beginning.
  • Print the deque after appending.
  • Apply pop() method to remove an element from the right end of the deque.
  • Print the deque after poping.
  • Apply popleft() method to remove an element from the left end of the deque.
  • Print the deque after applying popleft() function.
  • The Exit of the Program.

Below is the implementation:

# Import collections module using the import keyword
import collections
# Give the list as static input and store it in a variable.
gvn_lst = [5, 6, 7]
# Pass the given list as an argument to the deque() function for initializing the
# deque and store it in another variable.
dequ = collections.deque(gvn_lst)
# Append some random element to the deque using the append() function to insert an
# element at the right end of a deque.
dequ.append(8)
# Print the deque after appending right.
print("After appending at the right, the deque = ")
print(dequ)
# Append left some random element to the deque using the appendleft() function to
# insert an element at the left end of a deque. It inserts an element at
# the beginning.
dequ.appendleft(1)
# Print the deque after appending left.
print("After appending at the left, the deque = ")
print(dequ)
# Apply pop() method to remove an element from the right end of the deque.
dequ.pop()
# Print the deque after poping.
print("After deleting from the right, the deque = ")
print(dequ)
# Apply popleft() method to remove an element from the left end of the deque.
dequ.popleft()
# Print the deque after applying popleft() function.
print("After deleting from the left, the deque = ")
print(dequ)

Output:

After appending at the right, the deque = 
deque([5, 6, 7, 8])
After appending at the left, the deque = 
deque([1, 5, 6, 7, 8])
After deleting from the right, the deque = 
deque([1, 5, 6, 7])
After deleting from the left, the deque = 
deque([5, 6, 7])

2)index(), insert(), count() ,remove() Operations on deque

Approach:

  • Import collections module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the deque() function for initializing the deque and store it in another variable.
  • Print the index of the first occurrence of an element using the index() method by passing the element, starting, and ending value as the arguments.
  • Pass the position and some random value as the arguments to the insert() method to insert an element at the specified position.
  • Print the deque after inserting.
  • Count the frequency of an element using the count() method by passing the value as an argument and print the result.
  •  Remove the first occurrence of an element using the remove() method by passing the element as an argument.
  • Print the deque after removing the first occurrence of a specified element.
  • The Exit of the Program.

Below is the implementation:

# Import collections module using the import keyword
import collections
# Give the list as static input and store it in a variable.
gvn_lst = [5, 6, 5, 7, 6, 5, 8, 3]
# Pass the given list as an argument to the deque() function for initializing the
# deque and store it in another variable.
dequ = collections.deque(gvn_lst)
# Print the index of first occurrence of an element using the index() method by passing
# the element, starting and ending value as the arguments.
print("The index of first occurrence of 5 = ")
print(dequ.index(5, 1, 6))
# Pass the position and some random value and as the arguments to the insert() method
# to insert an element at the specified position.
dequ.insert(5, 9)
# Print the deque after inserting.
print("After inserting 9 in the 6th position the deque becomes: ")
print(dequ)
# Count the frequency of an element using the count() method by passing the value
# as an argument and print it.
print("In deque, the frequency of 5 =  ")
print(dequ.count(5))
# Remove the first occurrence of an element using the remove() method by passing
# the element as an argument.
dequ.remove(6)
# Print the deque after removing the first occurrence of a specified element.
print("After removing the first occurrence of 6, the deque = ")
print(dequ)

Output:

The index of first occurrence of 5 = 
2
After inserting 9 in the 6th position the deque becomes: 
deque([5, 6, 5, 7, 6, 9, 5, 8, 3])
In deque, the frequency of 5 =  
3
After removing the first occurrence of 6, the deque = 
deque([5, 5, 7, 6, 9, 5, 8, 3])

3)extend(), extendleft(), rotate() ,reverse() Operations on deque

Approach:

  • Import collections module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the deque() function for initializing the deque and store it in another variable.
  • Add some random numbers to the right end to the deque using the extend() method by passing the list as an argument.
  • Print the deque after extending to the right end.
  • Add some random numbers to the left end to the deque using the extendleft() method by passing the list as an argument.
  • Print the deque after extending to the left end.
  • Rotate the deque by left/right using the rotate() method by passing some value as an argument. The deque rotates by 2 to the left.
  • Print the deque after rotation by 2 to the left.
  • Reverse the deque by using the reverse() method.
  • Print the deque after reversing.
  • The Exit of the Program.

Below is the implementation:

# Import collections module using the import keyword
import collections
# Give the list as static input and store it in a variable.
gvn_lst = [5, 6, 7]
# Pass the given list as an argument to the deque() function for initializing the
# deque and store it in another variable.
dequ = collections.deque(gvn_lst)
# Add some random numbers to the right end to the deque using the extend()
# method by passing the list as an argument.
dequ.extend([8, 9, 10])
# Print the deque after extending to the right end.
print("After extending to the right end, the deque = ")
print(dequ)
# Add some random numbers to the left end to the deque using the extendleft()
# method by passing the list as an argument.
dequ.extendleft([11, 12, 13])
# Print the deque after extending to the left end.
print("After extending to the left end(beginning), the deque = ")
print(dequ)
# Rotate the deque by left/right using the rotate() method by passing some value as
# an argument.
# The deque rotates by 2 to the left.
dequ.rotate(-2)
# Print the deque after rotation by 2 to the left.
print("After rotation by 2 to the left, the deque = ")
print(dequ)
# Reverse the deque by using the reverse() method.
dequ.reverse()
# Print the deque after reversing.
print("After reversing, the deque = ")
print(dequ)

Output:

After extending to the right end, the deque = 
deque([5, 6, 7, 8, 9, 10])
After extending to the left end(beginning), the deque = 
deque([13, 12, 11, 5, 6, 7, 8, 9, 10])
After rotation by 2 to the left, the deque = 
deque([11, 5, 6, 7, 8, 9, 10, 13, 12])
After reversing, the deque = 
deque([12, 13, 10, 9, 8, 7, 6, 5, 11])