Deque in Python

The Queue is a core library that allows the users to define a list based on the FIFO (First In, First Out) principle. In contrast, the Deque in Python owns the opposite principle: LIFO (Last in, First Out) queue. In the following tutorial, we will only understand what Deque in Python is with some examples.

So, let's get started.

Understanding the Deque in Python

A Deque, also known as a Double-ended queue, has the attribute of inserting and deleting data elements from either end. The deque module is a segment of the library known as collections. It contains the attributes to add and remove data elements that can be invoked directly with parameters. In order to declare a deque, we have to import the collections library first.

Let us consider the following syntax to understand how the deque module works in Python.

Syntax:

Explanation:

In the above snippet of code, we have imported the deque module from the collections library and declared the deque by assigning the name of the list, i.e., list_name in the above case, to the deque() module. Here we can also observe that we do not need any class in order to implement these in-built methods. They can be implemented directly.

Let us consider a simple example based on the deque module.

Example:

Output:

deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya'])

Explanation:

In the above example, we have imported the deque module from the collections library. We have then defined a fruit list as a deque using the deque module, specifying some of the fruit names. We have then printed the declared Deque for the users. As a result, the declared Deque containing a bunch of fruit names is printed successfully.

Now, let us understand various Operations on Deque.

Some Operations on Deque

There are various Operations that can be used in Deque. Some of them are listed below with their descriptions:

S. No.OperationDescription
1append()The append() function is utilized for the addition of the data element in its parameter to the right end of the deque.
2appendleft()The appendleft() function is utilized for the addition of the data element in its parameter to the left end of the deque.
3pop()The pop() function is utilized for the deletion of the data element from the right end of the deque.
4popleft()The popleft() function is utilized for the deletion of the data element from the right end of the deque.
5index(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.
6insert(i, x)The insert() function is utilized to insert the value described in the parameter 'x' at index number 'i' mentioned in parameters.
7remove()The remove() function is utilized to remove the first occurrence of the value specified in the parameters.
8count()The count() function is utilized to count the total number of occurrences of the value specified in the parameters.
9extend(iterable)The extend() function is utilized to insert multiple data elements at the right end of the deque. The parameter passed is iterable.
10extendleft(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.
11reverse()The reverse() function is utilized to reverse the Order of deque data elements.
12rotate()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 us consider some examples based on the deque module.

Example:

Output:

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])

Explanation:

In the above snippet of code, we have imported the collections library and declared a deque. We have then used the operations like append() and appendleft() in order to insert the some data elements to both ends of the deque and printed the modified deque for the users. Similarly, we have then used the operations such as pop() and popleft() in order to remove the data elements from both ends of the deque and printed the resultant deque for the users.

Example:

Output:

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'])

Explanation:

In the above snippet of code, we have again imported the collections library and declared a deque. We have then used the index() operation in order to check the first occurrence of the data element 'Feb' between the index numbers 2 and 7, respectively. We have then used the insert() operation in order to insert the data element 'Jan' at the 4th position. We have then used the count() operation in order to count the occurrence of the data element 'Feb' in the deque. At last, we have used the remove() operation in order to remove the first occurrence of the data element 'Mar' from the deque and printed the resultant deque for the users.






Latest Courses