Deque in Python

Deck operations:

1. append () : — This function is used to insert a value in its argument to the right end deque.

2. appendleft () : — This function is used to insert the argument value into the left end deque.

3. pop () : — This function is used to remove an argument from the right end deque.

4. popleft () : — This function is used to remove an argument from the left end deque.

# Python code to demonstrate how it works
# append (), appendleft (), pop () and popleft ()

 
# import & quot; collections & quot; for deque operations

import collections

 
# deque initialization

de = collections.deque ([ 1 , 2 , 3 ])

  
# using append () to insert an element on the right end
# inserts 4 at the end of the deque

de.append ( 4 )

 
# printing the modified form

print ( " The deque after appending at right is: " )

print (de)

 
# using appendleft () to insert an element at the right end
# insert 6 at the beginning of the deque

de .appendleft ( 6 )

 
# print the modified form

print ( "The deque after appending at left is:" )

print (de)

 
# using pop () to remove an element from the right end
 # removes 4 from the right end of the deque
de.pop ()

 
# printing the modified form

print ( "The deque after deleting from right is:" )

print (de)

 
# using popleft () to remove an element from the left end
# remove 6 from the left end of the deque
de.popleft ()

 
# print modified form

print ( "The deque after deleting from left is: " )

print (de)

Exit:

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

5. index (ele, beg, end) : — This function returns the first index of the value specified in the arguments, starting from the beginning to the end index.

6. insert (i, a) : — This function inserts the value specified in the arguments (a), at the index (i) specified in the arguments.

7. remove () : — This function removes the first occurrence of the value specified in the arguments.

8. count () : — This function counts the number of occurrences of the value specified in the arguments.

 

# Python code to demonstrate how it works
# insert (), index (), remove (), count ()

 
# import & quot; collections & quot; for deque operations

import collections

 
# deque initialization

de = collections.deque ([ 1 , 2 , 3 , 3 , 4 , 2 , 4 ])

 
# using index (), to print the first occurrence 4

print ( "The number 4 first occurs at a position : " )

print (de. index ( 4 , 2 , 5 ))

  
# using insert () to insert the value 3 at the 5th position

de.insert ( 4 , 3 )

 
# print the modified form

print ( "The deque after inserting 3 at 5th position is: " )

print (de )

 
# using count () to count 3 occurrences

print ( " The count of 3 in deque is: " )

print (de.count ( 3 ))

 
# using remove () to remove the first occurrence 3

de.remove ( 3 )

 
# print the modified form

print ( "The deque after deleting first occurrence of 3 is:" )

print (de)

Exit:

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

9. extension (iterable) : — This function is used to add multiple values ​​at the right end deque. The argument passed is iterative.

10. exteleft (iterable) : — this function is used to add multiple values ​​to the left side of the deque. The passed argument is iterative.  Order canceled as a result of left addition.

11. reverse () : — This function is used to reorder deque elements.

12. rotate () : — This function rotates the deque by the number given in the arguments.  If the specified number is negative, rotation is to the left. The rest of the rotation is to the right.

# Python code to demonstrate how it works
# extended (), exteleft (), rotate (), reverse ()

 
# import & quot; collections & quot; for deque operations

import collections

 
# initializing deque

de = collections.deque ([ 1 , 2 , 3 ,])

  
# using extend () to add numbers to right end
# adds 4,5,6 to the right end

de.extend ([ 4 , 5 , 6 ])

  
# printing the modified form

print ( "The deque after extending deque at end is:" )

print (de)

 
# using exteleft ( ) to add numbers to the left end
# adds 7,8,9 to the right end

de.extendleft ([ 7 , 8 , 9 ])

 
# print the modified form

print ( "The deque after extending deque at beginning is:"

print (de)

 
# using rotate () to rotate the deck
# rotate 3 to the left

de.rotate ( - 3 )

 
# print modified form

print ( "The deque after rotating deque is: " )

print (de)

 
# use reverse () to rotate the deck
de.reverse ()

 
# printing the modified form

print ( "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]) 

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.