Deque in Python

PythonProgrammingServer Side Programming

The Deque is basically a generalization of stack and queue structure, where it is initialized from left to right. It uses the list object to create a deque.It provides O(1) time complexity for popping and appending.

The Dequeis a standard library class, which is located in the collections module.

To use it at first we need to import it the collections standard library module.

import collections

In this section we will see some functions of the Deque class

The Appending functions on Deque

There are two different types of append. The append() method is used to add elements at the right end of the queue, and appendleft() method is used to append the element at the left of the queue.

Example Code

import collections as col
#Insert some elements into the queue at first
my_deque = col.deque('124dfre')
   print('Dequeue: ' + str(my_deque))
      #insert x at right and B at left
      my_deque.append('x')
      my_deque.appendleft('B')
   print('Dequeue after appending: ' + str(my_deque))

Output

Dequeue: deque(['1', '2', '4', 'd', 'f', 'r', 'e'])
Dequeue after appending: deque(['B', '1', '2', '4', 'd', 'f', 'r', 'e', 'x'])

The Popping functions on Deque

Like appending, there are two different types of pop functions. The pop() method is used to remove and return the right most element from the queue, and popleft() method is used to remove and return left most element from the queue.

Example Code

import collections as col
#Insert some elements into the queue at first
my_deque = col.deque('124dfre')
   print('Dequeue: ' + str(my_deque))
      #delete item from right and left
      item = my_deque.pop()
   print('Popped Item: ' + str(item))
      item = my_deque.popleft()
   print('Popped Item: ' + str(item))
print('Dequeue after pop operations: ' + str(my_deque))

Output

Dequeue: deque(['1', '2', '4', 'd', 'f', 'r', 'e'])
Popped Item: e
Popped Item: 1
Dequeue after pop operations: deque(['2', '4', 'd', 'f', 'r'])

The item related functions in Deque

Some functions in Deque are used to get information related to items. There are some functions like index(), count() etc. The index method is used to get the index of the first occurrence an element. When no argument is passed with the element, it will choose the entire list, when a certain limit is specified, it checks the index in that limit. On the other hand the count() method counts the frequency of an item in the Deque.

Example Code

import collections as col
#Insert some elements into the queue at first
my_deque = col.deque('AABCDDEFD')
   print('Dequeue: ' + str(my_deque))
      #find the index of D
   print('Index of D:' + str(my_deque.index('D')))
print('Index of D in range 5 to 8 is: ' + str(my_deque.index('D', 5, 8)))
#Count the number of occurrences
   print('Occurrences of A: ' + str(my_deque.count('A')))
print('Occurrences of D: ' + str(my_deque.count('D')))

Output

Dequeue: deque(['A', 'A', 'B', 'C', 'D', 'D', 'E', 'F', 'D'])
Index of D:4
Index of D in range 5 to 8 is: 5
Occurrences of A: 2
Occurrences of D: 3

The insert() and remove() functions in Deque

We have already seen the append and pop functions in the Deque for inserting and deleting the elements respectively. There are another two methods related to insertion and deletion. The insert() method is used to insert a number. In this case we can provide the index for inserting. So on a specified location, the item can be inserted. And the remove() method is used to remove the first occurrence of an element.

Example Code

import collections as col
#Insert some elements into the queue at first
my_deque = col.deque('AABCDDEFD')
print('Dequeue: ' + str(my_deque))
#Insert letter G and H into the position 5, 7 respectively
my_deque.insert(5, 'G')
my_deque.insert(7, 'H')
print('Dequeue after inserting: ' + str(my_deque))
#Delete first occurrence of letter D
my_deque.remove('D')
print('Dequeue after removing: ' + str(my_deque))

Output

Dequeue: deque(['A', 'A', 'B', 'C', 'D', 'D', 'E', 'F', 'D'])
Dequeue after inserting: deque(['A', 'A', 'B', 'C', 'D', 'G', 'D', 'H', 'E', 'F', 'D'])
Dequeue after removing: deque(['A', 'A', 'B', 'C', 'G', 'D', 'H', 'E', 'F', 'D'])

Extending functions in Deque

The extending functions are used to add multiple elements into Deque. We can use collections like lists, tuples to provide multiple values. There are two types of extending functions. The extend() method is used to add elements to the right, it is similar to the repetitive append() function. And the extendleft() method is used to add elements to the left, it is similar to the repetitive appendleft() function.

Example Code

import collections as col
#Insert some elements into the queue at first
my_deque = col.deque('AABCDDEFD')
print('Dequeue: ' + str(my_deque))
#Extend by adding 1, 3, 5, 7 to the right and x, y, z to the left
my_deque.extend([1, 3, 5, 7])
my_deque.extendleft(['x', 'y', 'z'])
print('Dequeue after Extending: ' + str(my_deque))

Output

Dequeue: deque(['A', 'A', 'B', 'C', 'D', 'D', 'E', 'F', 'D'])
Dequeue after Extending: deque(['z', 'y', 'x', 'A', 'A', 'B', 'C', 'D', 'D', 'E', 'F', 'D', 1, 3, 5, 7])

Reversing and Rotating functions in Deque

We can reverse the sequence of the dequeuer using the reverse() method. There is another method called rotate(). Using the rotate method, the deque can be rotated with the number specified as argument. If the argument is positive, it rotates right, and for negative number it will be left rotate.

Example Code

import collections as col
#Insert some elements into the queue at first
my_deque = col.deque('AABCDDEFD')
print('Dequeue: ' + str(my_deque))
my_deque.reverse()
print('Deque after Reversing:' + str(my_deque))
#rotate to the right, 3 elements
my_deque.rotate(3)
print('Deque after rotating:' + str(my_deque))

Output

Dequeue: deque(['A', 'A', 'B', 'C', 'D', 'D', 'E', 'F', 'D'])
Deque after Reversing:deque(['D', 'F', 'E', 'D', 'D', 'C', 'B', 'A', 'A'])
Deque after rotating:deque(['B', 'A', 'A', 'D', 'F', 'E', 'D', 'D', 'C'])
raja
Published on 06-Nov-2018 17:31:04
Advertisements