Python: 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
7. Create a Deque and Append/Remove Elements, Then Reverse It
Write a Python program to create a deque and append a few elements to the left and right. Next, remove some elements from the left and right sides and reverse the deque.
Sample Solution:
Python Code:
Sample Output:
deque(['Red', 'Green', 'White']) Adding to the left: deque(['Pink', 'Red', 'Green', 'White']) Adding to the right: deque(['Pink', 'Red', 'Green', 'White', 'Orange']) Removing from the right: deque(['Pink', 'Red', 'Green', 'White']) Removing from the left: deque(['Red', 'Green', 'White']) Reversing the deque: deque(['White', 'Green', 'Red'])
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to create a deque, add elements to both ends, and then remove elements from both sides before finally reversing the deque.
- Write a Python program to simulate a queue with a deque where items are appended to the right and removed from the left, then reverse the queue.
- Write a Python program to implement a function that performs a series of appendleft() and append() operations on a deque and then returns its reversed form.
- Write a Python program to build a deque from user input, modify it with a series of insertions and deletions, and then print it in reverse order.
Go to:
Previous: Write a Python program that accepts number of subjects, subject names and marks. Input number of subjects and then subject name, marks separated by a space in next line. Print subject name and marks in order of its first occurrence.
Next: Write a Python program to create a deque from an existing iterable object.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.