Updated April 12, 2023
Definition of Deque in Python
Python provides the Deque functionality to the user. The Deque means double-ended queue, in python, we can implement deque by using a collection module. Basically, it is a part of the collection library; in deque, we can add or remove the elements from both ends that we call append and pop operation. In deque, every append operation provides the 0(1) tie complexity and every pop operation provides the 0(n) time complexity. There is no need to use any class for implementation of deque in python; it uses the in-built methods directly.
Syntax:
from collections import deque
variable name=deque(['string1', 'string2', 'string3'])
print(variable name)
Explanation
In the above syntax, we use an in-built method to implement the deque, here first we need to import the deque from the collection data structure. After that we define the list with deque keywords as shown in the above syntax, the list contains the different strings that is string1, string2, and string3.
How does Deque work in Python?
Now let’s see how deque works in python as follows.
A deque is a double-ended component where components can be both inserted and deleted from either the left or the right end of the queue. Execution of a deque in Python is accessible in the collections module.
The below-mentioned parameters are used to implement the deque as follows.
Insertion
- append(item): This is used to add the item to the right end in deque.
- appendleft(item): This is used to add the item to the left end in deque.
- insert(specified index, value): This is used to add the value at the specified index given by the user.
- extend(list): This is used to add the multiple values to the right end in deque. It accepts a list of qualities as a contention.
- extendleft(list): This function works the same as extend() function, yet it inverts the list of qualities passed as the contention and afterward adds that list to the left side of the deque.
Deletion
- Pop(): It is used to remove items from the right side of deque.
- popleft(): It is used to remove items from the left side of deque.
- remove(value): It is used to remove items from the first occurrence of value that is mentioned.
Miscellaneous
- count(value): It is used to return the absolute number of occurrences of the mentioned value.
- index(e, start, end): It is used to search the given item from start to finish and return the file of the primary event.
- rotate(n): It is used to rotate the deque a number of times. A positive worth pivots it to one side, while a negative worth turns it to one side.
- invert(): It is used to reverse the request for the deque.
Examples
Now let’s see the different examples of deque in python as follows.
Example #1
Now first create a simple deque by using the following code as follows.
from collections import deque
data = deque(['stud_name', 'roll_name', 'stud_address','stud_contact_no'])
print(data)
Explanation
In the above example, we try to implement deque, here first we need to import the deque from the collection library. After that, we declare the deque and finally, we print the value of the data variable as shown in the above code. The end out of the above code we illustrate by using the following screenshot as follows.
Now let’s see the different operations of deque as follows.
Example #2
Now perform append(), appendleft(), pop() and popleft() as follows.
import collections
data=collections.deque([5,6,7])
data.append(8)
print("After append at right operation the deque is:")
print(data)
data.appendleft(9)
print("After appendleft the operation the deque is:")
print(data)
data.pop()
print("After deletion the operation the deque is:")
print(data)
data.popleft()
print("pop item from left side of deque:")
print(data)
Explanation
In the above example, we try to implement different functions. First, we declared the deque with an item as shown in the above code.
The first operation we perform append at the right side of the deque. The end out we illustrate by using the following screenshot as follows.
After that, we perform the appendleft operation on deque that means insert an item at the left side of the deque. The end out we illustrate by using the following screenshot as follows.
After that, we perform the pop operation on deque which means we delete items from the right side of the deque. The end out we illustrate by using the following screenshot as follows.
After that, we perform the popleft operation on deque which means we delete items from the left side of the deque. The end out we illustrate by using the following screenshot as follows.
Example #3
Now let’s see the index-related examples with operation as follows.
import collections
data = collections.deque([7, 8, 9, 4, 3, 1, 2])
# using index() to print the first occurrence of 8
print("The number 8 occurs index : ")
print(data.index(8, 1, 5))
# using insert() to insert the value 5 at 10th position
data.insert(5, 10)
# print modified deque
print("insert 10 item at 5th position : ")
print(data)
# using count()
print("count of 4 in deque is : ")
print(data.count(4))
# using remove()
data.remove(4)
# print modified deque
print("delete appearance of 4 is : ")
print(data)
Explanation In the above example, we try to implement index-related functions as shown in the above example. First, we perform the index() function. The end out we illustrate by using the following screenshot as follows.
After that, we perform the insert operation at a specified index. The end out we illustrate by using the following screenshot as follows.
After that we perform the count() and remove() function. The end out we illustrate by using the following screenshot as follows.
Now perform extend(), rotate() and reverse() function as follows.
import collections
data = collections.deque([7, 8, 9, ])
data.extend([1, 2, 3])
print("The deque at right side: ")
print(data)
data.extendleft([4, 5, 6])
print("The deque at left side: ")
print(data)
data.rotate(-4)
print("After rotation operation the deque is : ")
print(data)
data.reverse()
print("The reversing deque is : ")
print(data)
Explanation
In the above example, we implement different operations as follows.
extend() function it extends the list at the right side. After we use rotate() and reverse() function as shown in above example. The end out we illustrate by using the following screenshot as follows.
Conclusion
We hope from this article you learn the Deque in python. From the above article, we have learned the basic syntax of Deque and we also see different examples of Deque. From this article, we learned how and when we use the Deque in python.
Recommended Articles
We hope that this EDUCBA information on “Deque in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.