Thursday 6 October 2022

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. 

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:

  1. # importing the collections library  
  2. for deque operations  
  3. import collections  
  4.   
  5. # declaring the deque  
  6. my_deque = collections.deque([1020304050])  
  7.   
  8. # using the append() function to add   
  9. # data element at right end  
  10. # inserting 60 at the end of the deque  
  11. my_deque.append(60)  
  12.   
  13. # printing the resultant deque  
  14. print( "The deque after appending at right: " )  
  15. print( my_deque )  
  16.   
  17. # using the appendleft() function to add  
  18. # data element at left end  
  19. # inserting 70 at the starting of the deque  
  20. my_deque.appendleft(70)  
  21.   
  22. # printing the resultant deque  
  23. print( "The deque after appending at left: " )  
  24. print( my_deque )  
  25.   
  26. # using the pop() function to remove  
  27. # data element from the right end  
  28. # removing 60 from the right end of deque  
  29. my_deque.pop()  
  30.   
  31. # printing the resultant deque  
  32. print( "The deque after removing from right: " )  
  33. print( my_deque )  
  34.   
  35. # using the popleft() function to remove  
  36. # data element from the left end  
  37. # removing 70 from the left end of deque  
  38. my_deque.popleft()  
  39.   
  40. # printing the resultant deque  
  41. print("The deque after removing from left: " )  
  42. print( my_deque )  

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


Source : https://www.javatpoint.com/deque-in-python

No comments:

Post a Comment