Skip to Content

Python – Rotate a Deque to the Right and Left

In this tutorial, we will look at how to rotate a deque (to the right and to the left) in Python with the help of some examples.

deque in Python

The collections module in Python comes with a deque class that you can use to implement a stack or queue data structure in Python. Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

In simple terms, you can think of a deque in Python as a list-like data structure but with efficient insertion and removal of items from either end (left and right). This makes them very useful when implementing a stack or queue functionality in Python.

What does it mean to rotate a deque?

You can think of rotating a deque (for example, by one step) to the right as removing an element from the right end of the deque and adding it to the left end of the deque.

Similarly, you can think of rotating a deque (for example, by one step) to the left as removing an element from the left end of the deque and adding it to the right end of the deque.

How to rotate a deque in Python?

The deque class comes with a number of built-in functions to help perform common operations (like adding and removing elements).

To rotate a deque in Python, use the deque rotate() function. Pass the number of steps you want to rotate the deque as an argument. The following is the syntax –

# rotate deque "queue"
queue.rotate(n)

It rotates the deque n steps to the right. The default value of n is 1. Use negative n to rotate the deque to the left.

Examples

Let’s now look at some examples of using the above syntax –

Example 1 – Rotate a deque to the right

First, let’s create a deque with some elements.

from collections import deque

# create a deque
queue = deque([1, 2, 3])
# print the deque
print(queue)

Output:

deque([1, 2, 3])

Here, we imported the deque class from the collections module and created a deque object, queue with three elements – 1, 2, and 3.

Now, let’s now rotate the deque created above one step to the right.

# rotate the deque
queue.rotate(1)
# print the deque
print(queue)

Output:

deque([3, 1, 2])

Here, we used the deque rotate() function to rotate (or shift) the values one step to the right.

We can similarly rotate the deque for higher values of n.

For example, let’s create a deque with 5 elements and rotate it three steps to the right.

# create a deque
queue = deque([1, 2, 3, 4, 5])
# print the deque
print(queue)
# rotate the deque
queue.rotate(3)
# print the deque
print(queue)

Output:

deque([1, 2, 3, 4, 5])
deque([3, 4, 5, 1, 2])

You can see that the values in the deque are rotated to the right by three steps.

Example 2 – Rotate deque to the left

To rotate a deque to the left, pass negative values for n. For example, to rotate the deque one step to the left, pass n=-1.

Let’s create the same deque from above.

# create a deque
queue = deque([1, 2, 3])
# print the deque
print(queue)

Output:

deque([1, 2, 3])

Here, we created a deque with three elements – 1, 2, and 3. Now, let’s rotate the values in this deque, one step to the left.

# rotate the deque
queue.rotate(-1)
# print the deque
print(queue)

Output:

deque([2, 3, 1])

The elements in the deque were rotated one step to the left.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.