All Courses

Python - Deque

Sanjeev Shah

3 years ago

Python - Deque | Insideaiml
In this article, we will try to learn about deque with some examples. 
Table Of Content
  • Python - Deque
  • What is Deque?
  • Deque Methods
  • Deque - extend() Method
  • Deque - extendleft() 
  • Method to Add Numbers To Left End 
  • Deque - rotate() Method To Rotate The Deque 
  • Deque - reverse() Method To Reverse The Deque
  • Conclusion
    

What is Deque?

          Deque is a double-ended queue. It has two ends, a front and a rear end. In Python, we can implement deque using a collection module. Deque provides a fast update, insert and delete operations than a list data structure. For appending and pop operation, deque provides time complexity O(1).
We can add or remove the item from deque either ends. The new item can be added from the front or rear end. The characteristics of the deque are the same as stack and queue, but it does not need to follow the Last In First Out (LIFO) or First In Last Out (FILO) order.
# importing "collections" module or deque operations
import collections

dq = collections.deque(["Mon","Tue","Wed"])

dq.append("Thu")

print ("Appended at right - ")
print (dq)

dq.appendleft("Sun")

print ("Appended at right at left is - ")
print (dq)

dq.pop()

print ("Deleting from right - ")
print (dq)

DoubleEnded.popleft()

print ("Deleting from left - ")
print (dq)
Output:
Appended at right - 
deque(['Mon', 'Tue', 'Wed', 'Thu'])
Appended at right at left is - 
deque(['Sun', 'Mon', 'Tue', 'Wed', 'Thu'])
Deleting from right - 
deque(['Sun', 'Mon', 'Tue', 'Wed'])
Deleting from left - 
deque(['Mon', 'Tue', 'Wed'])
   
  Recommended blog for you :  Introduction to Data Structures
     

Deque methods 

  • extend()
  • extendleft()
  • rotate()
  • reverse()

Deque - extend() Method

# importing "collections" module for deque operations 
import collections 

# initializing deque 
dq = collections.deque([10, 20, 30,]) 

# add numbers to right end using extend() method 
# adds 40,50,60 to right end 
dq.extend([40,50,60])

# extended Deque
print ("extended deque at the end is: ") 
print (dq)
Output:
The deque after extending deque at end is : 
deque([10, 20, 30, 40, 50, 60])

Deque - extendleft()

Method to Add Numbers To Left End  
# add numbers to left end using extendleft() method
# adds 70,80,90 to right end 
dq.extendleft([70,80,90]) 
  
# Ater extendleft deque operation 
print ("extended deque is : ") 
print (dq) 
Output:
The deque after extending deque at beginning is : 
deque([90, 80, 70, 10, 20, 30, 40, 50, 60])

Deque - rotate()

Method To Rotate The Deque
# to rotate the deque use rotate() method
# rotates by 3 to left 
dq.rotate(-3) 
  
# printing rorated deque 
print ("rotated deque is : ") 
print (dq) 
Output:
The deque after rotating deque is : 
deque([10, 20, 30, 40, 50, 60, 90, 80, 70])

Deque - reverse()

Method To Reverse The Deque
# to reverse the deque using reverse() method
dq.reverse() 
  
# printing reverse deque 
print (" reverse deque is: ") 
print (dq)
Output:
The deque after reversing deque is : 
deque([70, 80, 90, 60, 50, 40, 30, 20, 10])

Conclusion

          I hope you enjoyed reading this article and finally, you came to know about Python - Deque and its operations.
To know more about python programming language follow the insideaiml youtube channel.  
   
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…
     
Recommended Course for you
       
Recommended blog for you

Submit Review