
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to right rotate a list by n
In this article, we will discuss different methods used to right rotate a list from the given rotation number. A list has comma-separated values (items) of same type or different type between square brackets.
To right rotate a list by n, Python offers many ways such as using slicing, rotate() method of the deque object and others. Let's discuss these ways in detail with examples -
myList = [5, 20, 34, 67, 89, 94, 98, 110]
The following is the output if we assign n as 4 ?
89, 94, 98, 110, 5, 20, 34, 67
Right Rotate a List by n Using Bruteforce Approach
In this approach, we will transverse the list one by one and then put each of the element at required places in another list that is initialized as an empty list.
Example
In the example below, we define a function which will transverse the list and add values from n to the new the list and then call the function:
def rightRotate(lists, num): #Intialize list outputList = [] #Iterate to the last n elements in the sequence and append to the new list for item in range(len(lists) - num, len(lists)): outputList.append(lists[item]) for item in range(0, len(lists) - num): outputList.append(lists[item]) return outputList n = 4 myList = [5, 20, 34, 67, 89, 94, 98, 110] #Calling the function print("Updated List after rotation = ",rightRotate(myList, n))
The output returned by the above code is as follows -
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Right Rotate a List by n Using Slicing
In this approach, we will use slicing technique (method for extracting portion of a sequence like lists, strings, or tuples) to extract the last n elements from the list and then concatenate them with the first elements.
Example
In the example below, we will slice the list by n using the len() function and then right rotate rotate the list:
# Create a List myList =[5, 20, 34, 67, 89, 94, 98, 110] # The value of n for rotation position n = 4 # Rotating the List myList = (myList[len(myList) - n:len(myList)] + myList[0:len(myList) - n]) # Display the Update List after rotation print("Updated List after rotation = ",myList)
The output returned by the above code is as follows -
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Example : Using if Statement
Here, the if statement is used for cases where the rotation value n is larger than the length of the list. If n is greater then the length of the list, the if statement allows us to update n to be the remainder of n divided by the length of the list and then uses the slicing technique similar to the above example.
In the example, we follow the similar approach like the above one but additionally use the if statement -
# Create a List myList =[5, 20, 34, 67, 89, 94, 98, 110] # The value of n for rotation position n = 4 # Rotating the List if n>len(myList): n = int(n%len(myList)) myList = (myList[-n:] + myList[:-n]) # Display the Update List after rotation print("Updated List after rotation = ",myList)
The output returned by the above code is as follows -
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Example : Using extend()
To right rotate a list by n, we will define a function to calculate the number of positions to rotate by taking n modulo (%) the length of the list. For example, if n=7 and the length of the list is 5, then the effective rotation will be 2 (7%5 = 2). Further, we will use the extend() function to extend the list by appending all the first n items from the iterable to the end of the list and then delete the first n elements.
In the example program, we will define a function that takes the list and the number of rotations (n) as input parameters -
def right_rotate(lst, n): n = n % len(lst) lst.extend(lst[:n]) del lst[:n] return lst myList =[5, 20, 34, 67, 89, 94, 98, 110] n = 4 print("Updated List after rotation = ",right_rotate(myList, n))
The output returned by the above code is as follows -
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Right Rotate a List by n Using Recursion
In this approach, we will define a function that takes two parameters - list to be rotated and the number of positions to rotate (n). In the base case, if n is equal to 0, the list is returned as it is, and in the recursive case, the list is rotated by 1 position by concatenating the last element to the front and then calling the function again with n-1.
Example
In the example program below we will right rotate a list by n using recursion:
def right_rotate(list, n): if n == 0: return list else: return right_rotate(list[-1:] + list[:-1], n-1) myList = [5, 20, 34, 67, 89, 94, 98, 110] n = 4 print("Updated List after rotation = ", right_rotate(myList, n))
The output returned by the above code is as follows -
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Right Rotate a List by n Using Collections Module
Here, we will use deque which is a double-ended queue implementation in Python's collection module to right rotate a list by n.
The collections module provides special container types that you can use instead of regular containers like dictionaries, lists, sets, and tuples.
Example
In the example below, we will use rotate() method of the deque object to rotate the deque n steps to the right (in case if n is negative, the method rotates the list to the left) -
from collections import deque # Create a deque object from the list myList = [5, 20, 34, 67, 89, 94, 98, 110] outputList = deque(myList) # Rotate the deque by 4 outputList.rotate(4) print("Updated List after rotation =",list(outputList))
The output returned by the above code is as follows -
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]