English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Deque is a generalization of stack and queue structures, initialized from left to right. It uses list objects to create a deque, providing O(1)Time complexity.
Deque is a standard library class located incollectionsmodule.
First, we need to import it from the standard library module of collections.
import collections
In this section, we will see some functions of the Deque class
There are two different types of append. Theappend()
The method is used to add an element to the right end of the queue, whichappendleft()
The method is used to append an element to the left of the queue.
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 Result
Dequeue: deque(['1', ''2', ''4', 'd', 'f', 'r', 'e']) Dequeue after appending: deque(['B', '1', ''2', ''4', 'd', 'f', 'r', 'e', 'x'])
Like append, there are two different types of pop functions. Thepop()
The method is used to delete and return the rightmost element, whichpopleft()
The method is used to delete and return the leftmost element from the queue.
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 Result
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'])
Some functions of the Deque are used to obtain information related to items. There are some functions, such asindex()
,count()
etc. The index method is used to get the index of the first occurrence of an element. If no element is passed as a parameter, it will select the entire list, and after specifying a certain limit, it will check the index within that limit. On the other hand, thecount()
import collections as col #Insert some elements into the queue at first my_deque = col.deque('AABCDDEFD') print('Dequeue: ') + str(my_deque) method calculates the frequency of items in the deque. #find the index of D + print('Index of D: D 5 Index of D in range 8 print('Index of D in range + 5, 8D )) #Count the number of occurrences + print('Occurrences of A: ' A + print('Occurrences of D: '
Output Result
Dequeue: deque(['A', 'A', 'B', 'C', 'D', 'D', 'E', 'F', 'D']) D4 Index of D: 5 Index of D in range 8 to 5 is: 2 Occurrences of A: 3
insert()
andremove()
methodWe have already seen the append and pop functions in Deque, which are used to insert and delete elements, respectively. There are also two methods related to insertion and deletion. Theinsert()
The method is used to insert numbers. In this case, we can provide the insertion index. Therefore, we can insert the item at the specified position. And theremove()
The method is used to delete the first occurrence of an element.
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 the first occurrence of letter D my_deque.remove('D') print('Dequeue after removing: ' + str(my_deque)
Output Result
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'])
The extended function is used to add multiple elements to the Deque. We can use collections such as lists and tuples to provide multiple values. There are two extended functions. Theextend()
method is used to add elements to the right, similar to repeatingappend()
method. And theextendleft()
method is used to add elements to the left, similar to repeatingappendleft()
method.
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 Result
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])
we can use thisreverse()
method reverses the order of the queue. There is another methodrotate()
Using the rotate method, a deque can be rotated by a specified number of positions. If the parameter is positive, it rotates to the right; if negative, it rotates to the left.
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 Result
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'])