- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 the elements of an array
When it is required to right rotate the elements of a list, the elements are iterated over, and a last element is assigned a value, after which the elements are iterated over, and an element is swapped.
Below is a demonstration of the same −
Example
my_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] n = 3 print("The value of n has been initialized to") print(n) print("The list is :") print(my_list) print("List is being right rotated by 3 elements...") for i in range(0, n): last_elem = my_list[len(my_list)-1] for j in range(len(my_list)-1, -1, -1): my_list[j] = my_list[j-1] my_list[0] = last_elem print() print("List after right rotation is : ") for i in range(0, len(my_list)): print(my_list[i])
Output
The value of n has been initialized to 3 The list is : [31, 42, 13, 34, 85, 0, 99, 1, 3] List is being right rotated by 3 elements... List after right rotation is : 99 1 3 31 42 13 34 85 0
Explanation
A list is defined, and is displayed on the console.
The value of n is defined and is displayed on the console.
The list is iterated over, and the last element is assigned a value.
The list is iterated over again, and the step size is defined as -1, and it is specified to go till the last element of the list.
The last element is assigned to the first position of the list.
The list would have been rotated by three positions.
This is displayed as output on the console.
- Related Articles
- Python program to left rotate the elements of an array
- Python program to cyclically rotate an array by one
- Python program to right rotate a list by n
- Python program to print the duplicate elements of an array
- Java Program to shift array elements to the right
- Java program to program to cyclically rotate an array by one
- Java program to cyclically rotate an array by one.
- Java Program to Rotate Matrix Elements
- Golang Program To Rotate Matrix Elements
- Java Program to Rotate Elements of a List
- Python program to print the elements of an array in reverse order
- Python program to sort the elements of an array in ascending order
- Python program to sort the elements of an array in descending order
- Python program to print the elements of an array present on odd position
- Python program to print the elements of an array present on even position

Advertisements