Home ยป Deque in Python

Deque in Python

The Deque is another name for a double-ended queue, in python, we can implement deque by using a collection module.

In a deque, we can add or remove the elements from both ends, these are called append and pop operations.

In a deque, every append operation provides the 0(1) tie complexity and every pop operation provides the 0(n) time complexity.

Deque Operations

Operation Description
append() This is used to add the item to the right end in deque.
appendleft() This is used to add the item to the left end in deque.
pop() This is used to remove items from the right side of deque
popleft() This is used to remove items from the left side of deque.
index(element, begin, end) This is used to search the given item from start to finish and return the file of the primary event.
insert(i, x) The insert() function is utilized to insert the value described in the parameter ‘x’ at index number ‘i’ mentioned in parameters.
remove() This is used to remove items from the first occurrence of value that is mentioned.
count() This is used to return the absolute number of occurrences of the mentioned value.
extend(iterable) This is used to add the multiple values to the right end in deque. It accepts a list of qualities as a contention.
extendleft(iterable) This function works the same as extend() function, but it inverts the list of qualities passed as the contention and afterward adds that list to the left side of the deque.
reverse() The reverse() function is utilized to reverse the Order of deque data elements.
rotate(n) This 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.Insertion

 

Examples

This is a basic example showing several operations

We create a basic deque collection with 3 items.

We then append an item to the left and then right and then removing them and the reverse the deque

import collections
# Create a deque
mydeque = collections.deque(["Mar","Apr","Jun"])
print (mydeque)

# Append to the right
print("Adding to the right: ")
mydeque.append("Jul")
print (mydeque)

# Append to the left
print("Adding to the left: ")
mydeque.appendleft("Feb")
print (mydeque)

# Remove from the right
print("Removing from the right: ")
mydeque.pop()
print (mydeque)

# Remove from the left
print("Removing from the left: ")
mydeque.popleft()
print (mydeque)

# Reverse the dequeue
print("Reversing the deque: ")
mydeque.reverse()
print (mydeque)

This is what was displayed

>>> %Run dequeexample1.py
deque(['Mar', 'Apr', 'Jun'])
Adding to the right: 
deque(['Mar', 'Apr', 'Jun', 'Jul'])
Adding to the left: 
deque(['Feb', 'Mar', 'Apr', 'Jun', 'Jul'])
Removing from the right: 
deque(['Feb', 'Mar', 'Apr', 'Jun'])
Removing from the left: 
deque(['Mar', 'Apr', 'Jun'])
Reversing the deque: 
deque(['Jun', 'Apr', 'Mar'])

This example demonstrates the following insert(), index(), remove(), count()

# demo of insert(), index(), remove(), count()
import collections
 
# initialize deque
mydeque = collections.deque([1, 2, 3, 3, 4, 2, 4, 1 , 2, 3, 4])
 
# using index() to print the first occurrence of 4
print ("The number 3 first occurs at a position : ")
print (mydeque.index(3,2,5))
 
# using insert() to insert the value 4 at 7th position
mydeque.insert(6,4)
 
# printing deque
print ("The deque after inserting 4 at 7th position is : ")
print (mydeque)
 
# using count() to count the occurrences of 3
print ("The count of 4 in deque is : ")
print (mydeque.count(4))
 
# using remove() to remove the first occurrence of 3
mydeque.remove(4)
 
# printing modified deque
print ("The deque after deleting first occurrence of 4 is : ")
print (mydeque)

Which displays the following

>>> %Run dequeexample2.py
The number 3 first occurs at a position : 
2
The deque after inserting 4 at 7th position is : 
deque([1, 2, 3, 3, 4, 2, 4, 4, 1, 2, 3, 4])
The count of 4 in deque is : 
4
The deque after deleting first occurrence of 4 is : 
deque([1, 2, 3, 3, 2, 4, 4, 1, 2, 3, 4])

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More