How do you rotate an element in python?

The rotation of a list has been discussed earlier also, but this particular article focuses on shorthands and various short techniques to achieve this in one-liners or one word. This operation is quite essential in a programmers life to achieve various tasks.

Let’s discuss different ways we can rotate a list.

Method #1 : Using Slicing
This particular method is the generic method and mostly employed to achieve this task and also been discussed in many articles as well. It works by just joining the later sliced part to the initial sliced part given the rotation number.

test_list = [1, 4, 6, 7, 2]

print ("Original list : " + str(test_list))

test_list = test_list[3:] + test_list[:3]

print ("List after left rotate by 3 : " + str(test_list))

test_list = test_list[-3:] + test_list[:-3]

print ("List after right rotate by 3(back to original) : "

                                         + str(test_list))

Output:

Original list : [1, 4, 6, 7, 2]
List after left rotate by 3 : [7, 2, 1, 4, 6]
List after right rotate by 3 ( back to original) : [1, 4, 6, 7, 2]

 
Method #2 : Using list Comprehension
This problem can also be solved by naive method, but its shorter implementation would be with the help of list comprehension. In this method, we just reassign the index to each value to specific position after rotation.

test_list = [1, 4, 6, 7, 2]

print ("Original list : " + str(test_list))

test_list = [test_list[(i + 3) % len(test_list)]

               for i, x in enumerate(test_list)]

print ("List after left rotate by 3 : " + str(test_list))

test_list = [test_list[(i - 3) % len(test_list)]

               for i, x in enumerate(test_list)]

print ("List after right rotate by 3(back to original) : " 

                                        + str(test_list))

Output:

Original list : [1, 4, 6, 7, 2]
List after left rotate by 3 : [7, 2, 1, 4, 6]
List after right rotate by 3(back to original) : [1, 4, 6, 7, 2]

 
Method #3 : Using collections.deque.rotate()
The collections module has deque class which provides the rotate(), which is inbuilt function to allow rotation. This is lesser known function but has a greater utility.

from collections import deque

test_list = [1, 4, 6, 7, 2]

print ("Original list : " + str(test_list))

test_list = deque(test_list)

test_list.rotate(-3)

test_list = list(test_list)

print ("List after left rotate by 3 : " + str(test_list))

test_list = deque(test_list)

test_list.rotate(3)

test_list = list(test_list)

print ("List after right rotate by 3(back to original) : "

                                        + str(test_list))

Output:

Original list : [1, 4, 6, 7, 2]
List after left rotate by 3 : [7, 2, 1, 4, 6]
List after right rotate by 3(back to original) : [1, 4, 6, 7, 2]


How do you rotate a value in Python?

Rotate a Python list.
Using deque rotate() function. The collections. ... .
Using numpy.roll() function. If you happen to be using NumPy already, you can use the roll() function that rotates the array elements along a given axis. ... .
Using Slicing..

Is there a rotate command in Python?

rotate is undoable, NOT queryable, and NOT editable. The rotate command is used to change the rotation of geometric objects. ... .

How do you rotate a list in Python clockwise?

Input : n = 2, List_1 = [1, 2, 3, 4, 5, 6].
Output : List_1 = [5, 6, 1, 2, 3, 4].
Explanation: We get output list after right rotating (clockwise) given list by 2..

What does rotate do in Python?

The rotate() method of Python Image Processing Library Pillow Takes the number of degrees as a parameter and rotates the image in Counter Clockwise Direction to the number of degrees specified.