Advertisement
nathanwailes

Deques

Jun 9th, 2024 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. """
  2. A deque (double-ended queue) is a data structure that allows insertion and removal of elements from both ends (front and back). This flexibility makes it useful for scenarios requiring efficient additions and deletions from both ends, such as:
  3.  
  4. - Implementing LRU (Least Recently Used) caches: Quickly remove the least recently used item from one end and add new items to the other.
  5. - Handling sequences of items: Suitable for scenarios like undo functionality in applications, where you might need to add actions to the front or back of a list.
  6. - Breadth-first search (BFS) in graph algorithms: Efficiently enqueue and dequeue nodes.
  7.  
  8. Deques provide O(1) time complexity for operations at both ends, making them more efficient than lists when such operations are needed frequently.
  9. """
  10. from collections import deque
  11.  
  12. # Creating a deque
  13. my_deque = deque([1, 2, 3, 4, 5])
  14.  
  15. # Creating an empty deque
  16. empty_deque = deque()
  17.  
  18. # Adding elements to the deque
  19. my_deque.append(6)                 # Add an element to the right end
  20. my_deque.appendleft(0)             # Add an element to the left end
  21.  
  22. # Removing elements from the deque
  23. right_element = my_deque.pop()     # Remove and return an element from the right end
  24. left_element = my_deque.popleft()  # Remove and return an element from the left end
  25.  
  26. # Accessing elements in the deque
  27. first_element = my_deque[0]        # Access the first element
  28. last_element = my_deque[-1]        # Access the last element
  29.  
  30. # Modifying elements in the deque
  31. my_deque[1] = 10                   # Modify the second element
  32.  
  33. # Checking for elements
  34. is_in_deque = 10 in my_deque       # Check if an element is in the deque
  35. is_not_in_deque = 100 not in my_deque  # Check if an element is not in the deque
  36.  
  37. # Getting the length of the deque
  38. length = len(my_deque)             # Get the number of elements in the deque
  39.  
  40. # Iterating through the deque
  41. for element in my_deque:
  42.     print(element)                 # Print each element in the deque
  43.  
  44. # Reversing the deque
  45. my_deque.reverse()                 # Reverse the elements in the deque in place
  46.  
  47. # Extending the deque
  48. my_deque.extend([7, 8, 9])         # Add multiple elements to the right end
  49. my_deque.extendleft([-1, -2, -3])  # Add multiple elements to the left end (added in reverse order)
  50.  
  51. # Rotating the deque
  52. my_deque.rotate(1)                 # Rotate the deque one step to the right
  53. my_deque.rotate(-2)                # Rotate the deque two steps to the left
  54.  
  55. # Clearing the deque
  56. my_deque.clear()                   # Remove all elements from the deque
  57.  
  58. # Copying the deque
  59. copied_deque = my_deque.copy()     # Create a shallow copy of the deque
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement