Q:

Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque.

0

Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import collections
# Create a deque
deque_colors = collections.deque(["Red","Green","White"])
print(deque_colors)
# Append to the left
print("\nAdding to the left: ")
deque_colors.appendleft("Pink")
print(deque_colors)
# Append to the right
print("\nAdding to the right: ")
deque_colors.append("Orange")
print(deque_colors)
# Remove from the right
print("\nRemoving from the right: ")
deque_colors.pop()
print(deque_colors)
# Remove from the left
print("\nRemoving from the left: ")
deque_colors.popleft()
print(deque_colors)
# Reverse the dequeue
print("\nReversing the deque: ")
deque_colors.reverse()
print(deque_colors)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now