Python Program to right rotate elements of array

In this simple python program, we need right rotate the elements in an array. It's an array-based python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to right rotate or shift elements in an array?

In this simple python program on Arrays, we need to Right Rotate the elements of an array. An array is a collection of elements of the same data type which is stored in sequential memory locations. We have to move each element to n number of times right, where the n is given by the user.

For example, consider an array A with 5 elements: A = [ 1, 2, 3, 4, 5]. Suppose we have the value for n is 3, then we have to shift the elements 3 times to right for the result. In the first iteration, we will shift the elements by one to right and append the last element of the array to the array's first position, and the array will be like A = [ 5, 1, 2, 3, 4]. In the second Iteration, we will shift the elements to the right once more and then append the last one to the first of the array. The array will then be like A = [ 4, 5, 1, 2, 3] and so on.

So applying that logic to this simple python program, we use nested for loops in python. One outer For loop, which loops from zero to the value of n and assigns the variable last with the array's last element. Outer for loop is continued till how many times the user wants to right rotate the array elements. Now we use the inner for loop in the reverse direction, which means, from the last element of the array minus one to zero decrementing by one and apply array[j] = array[j-1]. Then finally we append the first element as the variable.


STEP 1: initialize an array with some predefined values in it.

STEP 2: Add value to n to determine how many times we have to right shift the array elements.

STEP 3: Display the real array using the for loop and print statement in the python programming language.

STEP 4: Add the outer for loop from zero to n and apply a variable with the value of the last element of the array.

STEP 5: Use the inner for loop from the end of the array minus one to zero decrementing one.

STEP 6: Assign the value of array[j] = arr[j-1] and append the first element of the array from the variable.

STEP 7: Display the array after the right shift using for loop in python language.

Python Source Code

                                          arr = [1, 2, 3, 4, 5];     
n = 3;    # value of how many times the array should be right shifted

print("Original array: ");    # display the real array
for i in range(0, len(arr)):    
for i in range(0, n):    
    last = arr[len(arr)-1];    # add the last element of array to a variable
    for j in range(len(arr)-1, -1, -1):    
        arr[j] = arr[j-1];    # use the for loop and assign the value array[j] = array[j-1]
    arr[0] = last;    # append the first element of array from the variable
print("Array after right rotation: ");    # print array after rotation
for i in range(0, len(arr)):    


Original array:
[ 1, 2, 3, 4, 5]

Array after right rotation

[3, 4, 5, 1, 2]