How to rotate a list in Python

In this article, we will learn how to rotate a list in Python.
List rotation is an easy technique and it also comes quite handy to programmers.
Let us discuss some ways in which we can achieve rotation.

We will achieve our goal in 5 different ways.

  1. By traversing
  2. By slicing
  3. Using len() function
  4. Using List comprehensions
  5. We can use collections.deque.rotate()

Let’s see what would you like to use.

 

Rotate a Python list By traversing

This is the most basic method for rotating a list in Python.
As the name suggests, we traverse the list one by one. Then we put the element in its place. The example below shows this technique. We rotate the list by an integer n, which in this case is 3.
Time complexity: O(n)

def right_rotate(lists, n): 
    output_list = [] 
    x= len(lists)
    
    for item in range(x - n, x): 
        output_list.append(lists[item]) 
      
       
    for item in range(0, x - n):  
        output_list.append(lists[item]) 
          
    return output_list 
  
 
rotate_num = 3
list_1 = [10, 25, 36, 43, 52, 60] 
print("List:", list_1)
print("Rotated list: ",right_rotate(list_1, rotate_num))

Output:

List: [10, 25, 36, 43, 52, 60]
Rotated list:  [43, 52, 60, 10, 25, 36]

Rotate list By Slicing in Python

Slicing is another way to achieve rotation of a list. This is done by using the len() function.
In the example below, the list is sliced. The value of n, in this case, is 3 i.e. the number of slices. Therefore the list is rotated likewise.

n= 3
  
list_1 = [11, 22, 33, 44, 55] 
print("List:", list_1)

list_1 = (list_1[len(list_1) - n:len(list_1)]  
                 + list_1[0:len(list_1) - n]) 
print("Rotated list:", list_1)

Output:

List: [11, 22, 33, 44, 55]
Rotated list: [33, 44, 55, 11, 22]

Method 3

In this method, no extra function like len() is used. Below is an example of this method in which the value of n is 2 and the list is rotated accordingly.

n = 2
  
list_1 = [10, 20, 30, 40, 50, 60] 
print("List:", list_1)
list_1 = (list_1[-n:] + list_1[:-n]) 

print("Rotated list:",list_1)

Output:

List: [10, 20, 30, 40, 50, 60]
Rotated list: [50, 60, 10, 20, 30, 40]

Time complexity: O(n)

Method 4- Using List comprehensions

In this method, we change the indexing of a list by reassigning a new index to each element after rotation.
In the example below, the list is rotated by 3 in which new index values are assigned.

list_1 = [1, 4, 6, 7, 2] 
  
# printing original list  
print ("List : " + str(list_1)) 
  
list_1 = [list_1[(i + 3) % len(list_1)] 
               for i, x in enumerate(list_1)] 
  

print ("Rotated list : " + str(list_1)) 
  

Output:

List : [1, 4, 6, 7, 2]
Rotated list : [7, 2, 1, 4, 6]

Method 5- Using collections.deque.rotate()

In Python, there exists a collection module that contains a deque class. This class has an inbuilt rotate() function.
In the example below, we have used the inbuilt function rotate().

from collections import deque 
  
list_1 = [11, 22, 33, 44, 55]  
print ("List : " + str(list_1)) 
  
list_1 = deque(list_1) 
list_1.rotate(-3) 
list_1 = list(list_1) 
  
print ("Rotated List: " + str(list_1)) 
  

Output:

List : [11, 22, 33, 44, 55]
Rotated List: [44, 55, 11, 22, 33]

These are five easy methods to rotate a given list in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *