Deque in Python

09 May, 2022
Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.
Example:
Python3
# Python code to demonstrate deque           from collections import deque       # Declaring deque queue = deque(['name','age','DOB'])        print(queue)
Output: 
deque(['name', 'age', 'DOB'])
Let’s see various Operations on deque : 
append():- This function is used to insert the value in its argument to the right end of the deque.
appendleft():- This function is used to insert the value in its argument to the left end of the deque.
pop():- This function is used to delete an argument from the right end of the deque.
popleft():- This function is used to delete an argument from the left end of the deque.
Python3
# Python code to demonstrate working of # append(), appendleft(), pop(), and popleft()  # importing "collections" for deque operationsimport collections  # initializing dequede = collections.deque([1,2,3])  # using append() to insert element at right end # inserts 4 at the end of dequede.append(4)  # printing modified dequeprint ("The deque after appending at right is : ")print (de)  # using appendleft() to insert element at left end # inserts 6 at the beginning of dequede.appendleft(6)  # printing modified dequeprint ("The deque after appending at left is : ")print (de)  # using pop() to delete element from right end # deletes 4 from the right end of dequede.pop()  # printing modified dequeprint ("The deque after deleting from right is : ")print (de)  # using popleft() to delete element from left end # deletes 6 from the left end of dequede.popleft()  # printing modified dequeprint ("The deque after deleting from left is : ")print (de)
Output: 
The deque after appending at right is : 
deque([1, 2, 3, 4])
The deque after appending at left is : 
deque([6, 1, 2, 3, 4])
The deque after deleting from right is : 
deque([6, 1, 2, 3])
The deque after deleting from left is : 
deque([1, 2, 3])
index(ele, beg, end):- This function returns the first index of the value mentioned in arguments, starting searching from beg till end index.
insert(i, a) :- This function inserts the value mentioned in arguments(a) at index(i) specified in arguments.
remove():- This function removes the first occurrence of value mentioned in arguments.
count():- This function counts the number of occurrences of value mentioned in arguments.
Python3
# Python code to demonstrate working of # insert(), index(), remove(), count()  # importing "collections" for deque operationsimport collections  # initializing dequede = collections.deque([1, 2, 3, 3, 4, 2, 4])  # using index() to print the first occurrence of 4print ("The number 4 first occurs at a position : ")print (de.index(4,2,5))  # using insert() to insert the value 3 at 5th positionde.insert(4,3)  # printing modified dequeprint ("The deque after inserting 3 at 5th position is : ")print (de)  # using count() to count the occurrences of 3print ("The count of 3 in deque is : ")print (de.count(3))  # using remove() to remove the first occurrence of 3de.remove(3)  # printing modified dequeprint ("The deque after deleting first occurrence of 3 is : ")print (de)
Output:  
The number 4 first occurs at a position : 
4
The deque after inserting 3 at 5th position is : 
deque([1, 2, 3, 3, 3, 4, 2, 4])
The count of 3 in deque is : 
3
The deque after deleting first occurrence of 3 is : 
deque([1, 2, 3, 3, 4, 2, 4])
extend(iterable):- This function is used to add multiple values at the right end of the deque. The argument passed is iterable.
extendleft(iterable):- This function is used to add multiple values at the left end of the deque. The argument passed is iterable. Order is reversed as a result of left appends.
reverse():- This function is used to reverse the order of deque elements.
rotate():- This function rotates the deque by the number specified in arguments. If the number specified is negative, rotation occurs to the left. Else rotation is to right.
Python3
# Python code to demonstrate working of # extend(), extendleft(), rotate(), reverse()  # importing "collections" for deque operationsimport collections  # initializing dequede = collections.deque([1, 2, 3,])  # using extend() to add numbers to right end # adds 4,5,6 to right endde.extend([4,5,6])  # printing modified dequeprint ("The deque after extending deque at end is : ")print (de)  # using extendleft() to add numbers to left end # adds 7,8,9 to left endde.extendleft([7,8,9])  # printing modified dequeprint ("The deque after extending deque at beginning is : ")print (de)  # using rotate() to rotate the deque# rotates by 3 to leftde.rotate(-3)  # printing modified dequeprint ("The deque after rotating deque is : ")print (de)  # using reverse() to reverse the dequede.reverse()  # printing modified dequeprint ("The deque after reversing deque is : ")print (de)
Output : 
The deque after extending deque at end is : 
deque([1, 2, 3, 4, 5, 6])
The deque after extending deque at beginning is : 
deque([9, 8, 7, 1, 2, 3, 4, 5, 6])
The deque after rotating deque is : 
deque([1, 2, 3, 4, 5, 6, 9, 8, 7])
The deque after reversing deque is : 
deque([7, 8, 9, 6, 5, 4, 3, 2, 1]) 
Python Programming Tutorial | Deque in Python | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersPython Programming Tutorial | Deque in Python | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:58•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=P6nskYVnQOE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
eddywade
punamsingh628700
hariaditya
deque
Python collections-module
Python-Data-Structures
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Iterate over a list in Python