- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 see how to right rotate a list from the given rotation number. A list has comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type
Let’s say the following is our input list −
myList = [5, 20, 34, 67, 89, 94, 98, 110]
The following is the output with n = 4 −
89, 94, 98, 110, 5, 20, 34, 67
Right rotate a list by n using slicing
Here, slicing is used to right rotate a list −
Example
# Create a List myList =[5, 20, 34, 67, 89, 94, 98, 110] print("List before rotation = ",myList) # 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)
Output
List before rotation = [5, 20, 34, 67, 89, 94, 98, 110] Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Right Rotate a List by n with if
Here, the if statement is used to right rotate a list −
Example
# Create a List myList =[5, 20, 34, 67, 89, 94, 98, 110] print("List before rotation = ",myList) # 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)
Output
List before rotation = [5, 20, 34, 67, 89, 94, 98, 110] Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
Right Rotate a List by n with for loop
Here, the for loop is used to right rotate a list −
Example
# The value of n for rotation position num = 4 # Create a List myList =[5, 20, 34, 67, 89, 94, 98, 110] print("List before rotation = ",myList) newlist = [] for item in range(len(myList) - num, len(myList)): newlist.append(myList[item]) for item in range(0, len(myList) - num): newlist.append(myList[item]) # Display the Update List after rotation print("Updated List after rotation = ",newlist)
Output
List before rotation = [5, 20, 34, 67, 89, 94, 98, 110] Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
- Related Articles
- Python program to rotate doubly linked list by N nodes
- JavaScript Program for Rotate Doubly linked list by N nodes
- Python Program to Rotate Elements of a List
- Python - Ways to rotate a list
- Program to rotate a linked list by k places in C++
- Python program to right rotate the elements of an array
- Program to rotate a string of size n, n times to left in Python
- Java Program to Rotate Elements of a List
- JavaScript Program for Rotate the matrix right by K times
- Python program to cyclically rotate an array by one
- Program to reverse a list by list slicing in Python
- Program to rotate square matrix by 90 degrees counterclockwise in Python
- Python program to find N largest elements from a list
- Rotate List Left by K in C++
- Program to create a list with n elements from 1 to n in Python

Advertisements