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

# 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
dq_object.append(2)
dq_object.append(4)
dq_object.append(6)
dq_object.append(8)
dq_object.append(10)
print("Deque before rotation:")
print(dq_object)
# Rotate once in positive direction
dq_object.rotate()
print("\nDeque after 1 positive rotation:")
print(dq_object)
# Rotate twice in positive direction
dq_object.rotate(2)
print("\nDeque after 2 positive rotations:")
print(dq_object)
```
```

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])
```

Flowchart:

## 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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## 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