Python’s deque module
The Queue is a fundamental library that adheres to the First In, First Out (FIFO) concept by allowing users to create lists in accordance with that idea. On the other hand, Python’s Deque implements the opposite concept, which is known as the LIFO (Last in, First Out) queue. In the next walkthrough, we will only grasp what Python’s Deque is by looking at several instances of it in action.
Ok, let’s get
started.
Understanding the Deque in Python
A Deque, which is also known as a Double-ended queue, has the capability of adding new data pieces and removing existing ones from either end of the queue. The deque module may be found inside the section of the library that is referred to as collections. It includes the properties that enable one to add or delete data pieces, and these attributes may be triggered directly with arguments. Importing the collections library is the first step that has to be taken before we can define a deque.
In order to have an understanding of the functionality of the deque module in Python, let us examine the following syntax. Syntax:
Explanation:
-
# importing the deque module
-
# from the collections library
-
from collections
import
deque
-
# declaring the deque
-
list_name = deque()
Symbol:
In the above bit of code, we imported the deque module from the collections library. Next, to declare the deque, we gave the deque() module the name of the list, which was list name in this particular example. Finally, we exited the deque() module. In this regard, we can also note that the implementation of these in-built methods does not need the use of any class on our part. They are capable of being executed immediately.
Let us take a look at a straightforward example using the deque module. Example:
-
# importing the deque module
-
# from the collections library
-
from collections
import
deque
-
-
# declaring the deque
-
fruit_list = deque([
‘Apple’
,
‘Mango’
,
‘Peaches’
,
‘Banana’
,
‘Papaya’
])
-
-
# printing the deque
-
print(fruit_list)
Output:
Explanation for
deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya'])
:
In the preceding example, we have brought in the deque module that is located in the collections library. After that, we have used the deque module to create a fruit list as a deque and have specified some of the fruit names in that list. After that, we printed the users’ stated Deque and handed it to them. As a direct consequence of this, the proclaimed Deque that is full with the names of many fruits is effectively printed.
Now, let’s talk about the several Operations that may be performed on
Deque.
Some Operations on Deque
At Deque, you have access to a wide variety of Operations to choose from. The following is a list of some of them along with their
descriptions:
S. No. | Operation | Description |
---|---|---|
1 | append() | The append() function is utilized for the addition of the data element in its parameter to the right end of the deque. |
2 | appendleft() | The appendleft() function is utilized for the addition of the data element in its parameter to the left end of the deque. |
3 | pop() | The pop() function is utilized for the deletion of the data element from the right end of the deque. |
4 | popleft() | The popleft() function is utilized for the deletion of the data element from the right end of the deque. |
5 | index(element, begin, end) | The index() function is utilized to return the first index value specified in the parameters, start the search from begin till end index. |
6 | insert(i, x) | The insert() function is utilized to insert the value described in the parameter ‘x’ at index number ‘i’ mentioned in parameters. |
7 | remove() | The remove() function is utilized to remove the first occurrence of the value specified in the parameters. |
8 | count() | The count() function is utilized to count the total number of occurrences of the value specified in the parameters. |
9 | extend(iterable) | The extend() function is utilized to insert multiple data elements at the right end of the deque. The parameter passed is iterable. |
10 | extendleft(iterable) | The extendleft() function is utilized to insert multiple data elements at the left end of the deque. The parameter passed is iterable. The Order is also reversed as an output of left appends. |
11 | reverse() | The reverse() function is utilized to reverse the Order of deque data elements. |
12 | rotate() | The rotate() function is utilized to rotate the deque by the number mentioned in parameters. If the mentioned number is a negative value, then the rotation occurs to the left. Else rotation is to the right. |
Now, let’s have a look at some examples that are derived from the deque module. Example:
-
# importing the collections library
-
#
for
deque operations
-
import
collections
-
-
# declaring the deque
-
my_deque = collections.deque([
10
,
20
,
30
,
40
,
50
])
-
-
# using the append() function to add
-
# data element at right end
-
# inserting
60
at the end of the deque
-
my_deque.append(
60
)
-
-
# printing the resultant deque
-
print(
“The deque after appending at right: ”
)
-
print( my_deque )
-
-
# using the appendleft() function to add
-
# data element at left end
-
# inserting
70
at the starting of the deque
-
my_deque.appendleft(
70
)
-
-
# printing the resultant deque
-
print(
“The deque after appending at left: ”
)
-
print( my_deque )
-
-
# using the pop() function to remove
-
# data element from the right end
-
# removing
60
from the right end of deque
-
my_deque.pop()
-
-
# printing the resultant deque
-
print(
“The deque after removing from right: ”
)
-
print( my_deque )
-
-
# using the popleft() function to remove
-
# data element from the left end
-
# removing
70
from the left end of deque
-
my_deque.popleft()
-
-
# printing the resultant deque
-
print(
“The deque after removing from left: ”
)
-
print( my_deque )
Output:
Explanation for
The deque after appending at right: deque([10, 20, 30, 40, 50, 60]) The deque after appending at left: deque([70, 10, 20, 30, 40, 50, 60]) The deque after removing from right: deque([70, 10, 20, 30, 40, 50]) The deque after removing from left: deque([10, 20, 30, 40, 50])
The line of code that you just saw contains an import of the collections library as well as a declaration of a deque. After that, we inserted some data pieces into both ends of the deque by using operations such as append() and appendleft(), and then we printed the changed deque out for the users. In a similar manner, we first removed the data components from both ends of the deque by using operations such as pop() and popleft(), and then we printed the resulting deque out for the users. Example:
-
# importing the collections library
-
import
collections
-
-
# declaring the deque
-
my_deque = collections.deque([
‘Jan’
,
‘Feb’
,
‘Mar’
,
‘Mar’
,
‘Feb’
,
‘April’
,
‘Feb’
])
-
-
# using the index() function to print
-
# the first occurrence of data element: Feb
-
print(
“The first occurs of ‘Feb’ at a position: ”
)
-
print( my_deque.index(
‘Feb’
,
2
,
7
) )
-
-
# using the insert() function to insert
-
# the data element
‘Jan’
at 4th position
-
my_deque.insert(
3
,
‘Jan’
)
-
-
# printing the resultant deque
-
print(
“The deque after inserting ‘Jan’ at 4th position: ”
)
-
print( my_deque )
-
-
# using the count() function to count
-
# the occurrences of data element
‘Feb’
-
print(
“The count of ‘Feb’ in deque: ”
)
-
print( my_deque.count(
‘Feb’
) )
-
-
# using the remove() function to remove
-
# the first occurrence of data element
‘Mar’
-
my_deque.remove(
‘Mar’
)
-
-
# printing the resultant deque
-
print(
“The deque after removing the first occurrence of ‘Mar’: ”
)
-
print( my_deque )
Output:
Explanation of
The first occurs of 'Feb' at a position: 4 The deque after inserting 'Jan' at 4th position: deque(['Jan', 'Feb', 'Mar', 'Jan', 'Mar', 'Feb', 'April', 'Feb']) The count of 'Feb' in deque: 3 The deque after removing the first occurrence of 'Mar': deque(['Jan', 'Feb', 'Jan', 'Mar', 'Feb', 'April', 'Feb'])
Format:
The line of code that you just saw contains yet another import of the collections library and a declaration of a deque. After that, we checked for the first occurrence of the data element ‘Feb’ between the index numbers 2 and 7 by using the index() procedure. These numbers represent the range in which we were looking. The data element ‘Jan’ was subsequently inserted into the list at the 4th place after we used the insert() procedure to do this. After that, we have made use of the count() technique in order to determine how many times the data element “Feb” appears in the deque. When all was said and done, we put the finishing touches on the deque by using the delete() method to get rid of the first occurrence of the data element “Mar” in the deque and then printing it out for the users.