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 −
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 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])
The list is : 11 12 23 34 65 Array after left rotating is : 34 65 11 12 23
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.