Python Deque
Deque stands for double-ended queue and is pronounced as “deck”. It is implemented in Python via the collection’s module. Deque and Lists data structures are simpler, while deque inserts and delta data from both ends and lists insertion and deletion data in the middle. The list has two pointers for each node while the list is similar to an array which has one pointer for each element.
This write-up follows the below content:
- What is Deque in Python?
- How to Perform Operation on Deque in Python?
- Appending Items Efficiently
- Popping Items Efficiently
- Accessing Items in a deque
- Finding the Size of a deque
- Add Multiple Values on a Deque
- Finding the Front and Back of a deque
What is Deque in Python?
The “deque” is a data type in Python that allows efficient adding and removing of elements from both sides. It is similar to a sequence, but it can be used to implement data structures like queues, stacks, and more that need fast access to both ends of the data.
Here is an example that is utilized to create the deque by using the “deque()” function of the collections module:
print(deque(['Name','Age','Height']))
The deque object has been created with three elements of the list Iterable:
How to Perform Operation on Deque in Python?
We perform various operations on deque, such as appending, popping, inserting, removing, and counting elements. Let’s perform these operations on various methods via the following example:
Appending Items Efficiently Using “append()” and “appendleft()” Methods
In this example code, the “append()” and the “appendleft()” methods are used to append an element from right and left. First, create the deque object by executing the below code:
de = collections.deque([5, 10, 15])
print(de)
The deque object has been created:
Now, take this code, to append “2” to the right end of the deque object using the “append()” method:
print("\nDeque After Appending at Right: ", de)
The integer has been appended to the deque:
The “appendleft()” method is used to append the integer in the deque from the left end:
print("Deque After Appending at Left: ", de)
Here is the output:
Popping Items Efficiently Using “pop()” and “popleft()” Methods
We can also pop the item from the deque using the pop() and popleft() methods. First, we need to create the deque object by executing this code:
de = collections.deque([5, 10, 15])
print(de)
The deque object has been created:
Next, the “pop()” method is used to delete/remove the element from the right end of the deque:
print("Deque After Deleting From Right: ", de)
As you can see, the deque after removing has been retrieved:
We can also remove the element from the left end of the deque object. To do this, the “popleft()” method is used in the following code:
print("Deque After Deleting From Left: ", de)
The below snippet shows the removal of an element from the left end of the deque:
Accessing Items in a deque Using “insert()” and “count()” and “remove()” Methods
The “insert()”, “count()”, and “remove()” methods are utilized in Python to access the items in a deque. To do so, first, use the below code, to create the deque object:
de = collections.deque([11, 22, 33, 33, 44, 22, 44])
print(de)
Deque object has been created:
First, we can use the “deque.index()” method that takes three parameters, element value, starting index, and closing index. This method finds the index of the element “44” in the deque within the specified index:
print(d1)
The first index of the specified value has been retrieved:
We can insert the specified value by passing the index and the value to the “deque.insert()” method. Here we can pass the “3” value to the index “4” of the deque:
print(de)
After inserting the deque contains the following value:
We can also count the total number of the element values found in the deque using the “deque.count()” method. In this case, the “deque.count()” is used to find the value of “33” in the given deque object:
print(d1)
The total count of the “33” value in deque is shown below:
Lastly, the “deque.remove()” method is used to remove the specified element value from the deque object. Take this code to remove the “33” from the deque object:
print(de)
Here is the output:
Note: Only remove the first occurrence.
Finding the Size of a deque Using the “len()” Method
The “len()” method is used to determine the size/length of the deque. First, create the deque object by importing the “collection” module at the start:
de = deque([10, 20, 30, 40, 50, 60])
print(de)
The deque object is shown below:
Next, use the “len()” method to print the size of the created deque data type:
The total size of the deque object is:
Add Multiple Values on Deque
Before adding multiple values on the deque object element, first, create the deque object by using the “collections.deque()” constructor:
de = collections.deque([5, 6, 7,])
print(de)
Deque object has been created:
Now, the “extend()” method is used to add multiple values to the deque object from the right end:
print(de)
The multiple values from the right end have been added:
We can also use the “extendleft()” method to add multiple values from the left end of the deque object:
print(de)
Here is the output:
We can use the “deque.rotate()” method to rotate the object by “-3” degrees. The direction of rotation depends on the coordinate system. Here is an example:
print(de)
The above code retrieves this output:
To reverse the direction of the deque object the “deque.reverse()” method is used in Python. Take this code to reverse the deque object:
print(de)
After reversing the deque object the following output displays:
Finding the Front and Back of a deque
We can also find the front and back values of the deque by using the deque variable and the index value. The “de[0]” retrieved the first element of the deque while the “-1” retrieved the last value of the deque:
de = deque([1, 2, 3, 4, 5, 6])
print(de)
print(de[0])
print(de[-1])
The front and back of the deque is shown below:
Conclusion
In Python, the “double-ended queue” named “deque” is a data structure that is utilized to append and pop items from both ends. The collection module provides the constructor “collection.deque()” to create the deque object from the list Iterable. We can perform various operations on the deque object by using multiple methods, such as insert(), extend(), and reverse(), etc. This tutorial provided multiple examples for understanding the Python deque.
Source: linuxhint.com