from collections import deque
# 1. Creating a Deque
d = deque(range(1, 11)) # Initialize deque with 10 elements
print("Initial deque:", d)
# 2. Adding Elements
d.append(11) # Add to the right
d.appendleft(0) # Add to the left
print("After append and appendleft:", d)
# 3. Removing Elements
d.pop() # Remove from the right
d.popleft() # Remove from the left
print("After pop and popleft:", d)
# 4. Extending a Deque
d.extend(range(12, 16)) # Add multiple elements to the right
d.extendleft([-3, -2, -1]) # Add multiple elements to the left (reversed order)
print("After extend and extendleft:", d)
# 5. Rotating the Deque
d.rotate(5) # Rotate right by 5
print("After rotating right by 5:", d)
d.rotate(-3) # Rotate left by 3
print("After rotating left by 3:", d)
# 6. Accessing Elements
print("First element:", d[0])
print("Last element:", d[-1])
# 7. Other Methods
print("Count of 5:", d.count(5)) # Count occurrences of 5
d.remove(5) # Remove the first occurrence of 5
print("After removing 5:", d)
d.clear() # Clear the deque
print("After clearing:", d)
# 8. Limiting the Size
d = deque(maxlen=10) # Create a deque with a maximum size of 10
d.extend(range(1, 11)) # Fill the deque with 10 elements
print("Deque with maxlen=10:", d)
# Add more elements to demonstrate overflow behavior
d.append(11) # This will remove the oldest element (1)
d.append(12) # This will remove the next oldest element (2)
print("Deque after adding elements beyond maxlen:", d)
# 9. Initializing an Empty Deque
empty_d = deque()
print("Empty deque:", empty_d)