| by Arround The Web | No comments

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?

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:

from collections import deque

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:

import collections

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:

de.append(2)

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:

de.appendleft(18)

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:

import collections

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:

de.pop()

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:

de.popleft()

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:

import collections

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:

d1 = de.index(44,2,5)

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:

de.insert(4,3)

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:

d1 = de.count(33)

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:

de.remove(33)

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:

from collections import deque

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:

print(len(de))

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:

import collections

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:

de.extend([8, 9, 10])

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:

de.extendleft([11, 12, 13])

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:

de.rotate(-3)

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:

de.reverse()

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:

from collections import 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.

Share Button

Source: linuxhint.com

Leave a Reply