
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 left rotate the elements of an array
When it is required to left rotate the elements of an array, the array can be iterated over, and depending on the number of left rotations, the index can be incremented that many times.
Below is a demonstration of the same −
Example
my_list = [11, 12, 23, 34, 65] n = 3 print("The list is : ") for i in range(0, len(my_list)): print(my_list[i]) for i in range(0, n): first_elem = my_list[0] for j in range(0, len(my_list)-1): my_list[j] = my_list[j+1] my_list[len(my_list)-1] = first_elem print() print("Array after left rotating is : ") for i in range(0, len(my_list)): print(my_list[i])
Output
The list is : 11 12 23 34 65 Array after left rotating is : 34 65 11 12 23
Explanation
A list is defined, and is displayed on the console.
The value for left rotation is defined.
The list is iterated over, and the index of elements in the list is incremented, and assigned to previous index of the same list.
Once it comes out of the loop, the first element (at the 0th index) is assigned to the last element.
This is the output that is displayed on the console.
- Related Questions & Answers
- Python program to right rotate the elements of an array
- Python program to cyclically rotate an array by one
- Java Program to shift array elements to the left
- Python program to print the duplicate 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
- Java Program to Rotate Matrix Elements
- Java program to cyclically rotate an array by one.
- Java program to program to cyclically rotate an array by one
- 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
- C++ program to find array after removal of left occurrences of duplicate elements
- Rotate Array in Python
- Python program to print the elements of an array present on odd position
Advertisements