Python: Rotate a Deque Object specified number (positive) of times

Python Collections: Exercise-13 with Solution

Write a Python program to rotate a Deque Object a specified number (positive) of times.

Sample Solution:

Python Code:

import collections
# declare an empty deque object
dq_object = collections.deque()
# Add elements to the deque - left to right
print("Deque before rotation:")
# Rotate once in positive direction
print("\nDeque after 1 positive rotation:")
# Rotate twice in positive direction
print("\nDeque after 2 positive rotations:")

Sample Output:

Deque before rotation:
deque([2, 4, 6, 8, 10])

Deque after 1 positive rotation:
deque([10, 2, 4, 6, 8])

Deque after 2 positive rotations:
deque([6, 8, 10, 2, 4])


Python Collections: Rotate a Deque Object specified number (positive) of times.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to count the number of times a specific element presents in a deque object.
Next: Write a Python program to rotate a deque Object specified number (negative) of times.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Given each iterable to construct a tuple by adding an index:

>>> a = ['Hello', 'world', '!']
>>> list(enumerate(a))
[(0, 'Hello'), (1, 'world'), (2, '!')]


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook