Rotate a list python – Python Program to Right Rotate a List by R Times

Rotate a list python: Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Right Rotation of a List:

Python rotate list: An array’s elements are shifted to the right when it is rotated to the right, as demonstrated in the image below. right rotation involves rotating the array’s elements clockwise to the provided number of places.

Examples:

Example1:

Input:

given list =[3, 9, 1, 2, 4, 5, 11, 23]
number of positions = 6

Output:

The list before rotating r times =  [3, 9, 1, 2, 4, 5, 11, 23]
The list after  rotating r times :  [1, 2, 4, 5, 11, 23, 3, 9]

Example2:

Input:

given list =  ['good', 'morning', 'this', 'is', 'btechgeeks']
number of positions = 3

Output:

The list before rotating r times = ['good', 'morning', 'this', 'is', 'btechgeeks']
The list after rotating r times : ['this', 'is', 'btechgeeks', 'good', 'morning']

Program to Right Rotate a List by r Times in Python

Rotate list python: There are several ways to right rotate the given list by r times to the right in Python some of them are:

Method #1:Using Indexing(Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the number of rotations r as static input and store it in another variable.
  • Pass the given list and number of rotations as arguments to the rightRotateList function which rotates the given list to the right by r positions.
  • Use for loop to loop r times.
  • Pass the given list to the rightRotateOne function inside the for loop which rotates the list to the right one time.
  • Inside the rightRotateOne function follow the below steps.
  • Initialize the last variable to the last element of the given list.
  • Traverse length in reverse order of given list -1 times using for loop and reversed range functions.
  • Initialize the next index element to the current index element inside the For loop.
  • After the loop Initialize the first value of the given list to the last value.
  • In this way, we rotate the given list to the right r times.
  • The Exit of the Program.

Below is the implementation:

# function which accepts the giveen list as argument
# and rotates the given list to the right by one time


def rightRotateByOne(given_list):
    # intializing last variable to the last element of the given list
    lastval = given_list[-1]
    # traversing length in reverse order of given list -1 times using for loop and reversed,range functions
    for i in reversed(range(len(given_list) - 1)):
      # initializing the next index element to the current index element
        given_list[i+1] = given_list[i]
    # Initialize the first value of the given list to the last value.
    given_list[0] = lastval


# function which accepts the given list and number
# of positions as arguments and rotate the given list r times here r=positions
def rightRotateList(given_list, positions):
    # Pass the given list to the rightRotateOne function inside the
    # for loop which rotates the list to the right one time.
    for numb in range(positions):
        rightRotateByOne(given_list)


# Driver Code
# Give the list as static input and store it in a variable.
given_list = [3, 9, 1, 2, 4, 5, 11, 23]
print('The list before rotating r times = ', given_list)
# Give the number of rotations r as static input and store it in another variable.
positions = 6
# Pass the given list and number of rotations as arguments to the rightRotateList function
# which rotates the given list to the right by r positions.
rightRotateList(given_list, positions)
# print the list after rotating to the right r times
print('The list after  rotating r times : ', given_list)

Output:

The list before rotating r times =  [3, 9, 1, 2, 4, 5, 11, 23]
The list after  rotating r times :  [1, 2, 4, 5, 11, 23, 3, 9]

The above solution has a time complexity of, where n is the size of the input and r is the number of rotations.

Method #2:Using Indexing(User Input)

i)Integer List

Approach:

  • Give the integer list as user input using map(),split(),list() and int functions.
  • Store it in a variable.
  • Give the number of rotations r as user input using int(input()) and store it in another variable.
  • Pass the given list and number of rotations as arguments to the rightRotateList function which rotates the given list to the right by r positions.
  • Use for loop to loop r times.
  • Pass the given list to the rightRotateOne function inside the for loop which rotates the list to the right one time.
  • Inside the rightRotateOne function follow the below steps.
  • Initialize the last variable to the last element of the given list.
  • Traverse length in reverse order of given list -1 times using for loop and reversed range functions.
  • Initialize the next index element to the current index element inside the For loop.
  • After the loop Initialize the first value of the given list to the last value.
  • In this way, we rotate the given list to the right r times.
  • The Exit of the Program.

Below is the implementation:

# function which accepts the giveen list as argument
# and rotates the given list to the right by one time


def rightRotateByOne(given_list):
    # intializing last variable to the last element of the given list
    lastval = given_list[-1]
    # traversing length in reverse order of given list -1 times using for loop and reversed,range functions
    for i in reversed(range(len(given_list) - 1)):
      # initializing the next index element to the current index element
        given_list[i+1] = given_list[i]
    # Initialize the first value of the given list to the last value.
    given_list[0] = lastval


# function which accepts the given list and number
# of positions as arguments and rotate the given list r times here r=positions
def rightRotateList(given_list, positions):
    # Pass the given list to the rightRotateOne function inside the
    # for loop which rotates the list to the right one time.
    for numb in range(positions):
        rightRotateByOne(given_list)


# Driver Code
# Give the list as user input using map(),split(),list() and int functions.
# Store it in a variable. 
given_list = list(
    map(int, input('Enter some random list separated by spaces = ').split()))
print('The list before rotating r times = ', given_list)
# Give the number of rotations r as user input using int(input())
# and store it in another variable.
positions = int(input('Enter some random number of positions = '))
# Pass the given list and number of rotations as arguments to the rightRotateList function
# which rotates the given list to the right by r positions.
rightRotateList(given_list, positions)
# print the list after rotating to the right r times
print('The list after rotating r times : ', given_list)

Output:

Enter some random list separated by spaces = 8 77 9 12 1 6 4 3 7 9
The list before rotating r times = [8, 77, 9, 12, 1, 6, 4, 3, 7, 9]
Enter some random number of positions = 4
The list after rotating r times : [4, 3, 7, 9, 8, 77, 9, 12, 1, 6]

The above solution has a time complexity of, where n is the size of the input and r is the number of rotations.

ii)String List

Approach:

  • Give the string list as user input using split(),list() functions.
  • Store it in a variable.
  • Give the number of rotations r as user input using int(input()) and store it in another variable.
  • Pass the given list and number of rotations as arguments to the rightRotateList function which rotates the given list to the right by r positions.
  • Use for loop to loop r times.
  • Pass the given list to the rightRotateOne function inside the for loop which rotates the list to the right one time.
  • Inside the rightRotateOne function follow the below steps.
  • Initialize the last variable to the last element of the given list.
  • Traverse length in reverse order of given list -1 times using for loop and reversed range functions.
  • Initialize the next index element to the current index element inside the For loop.
  • After the loop Initialize the first value of the given list to the last value.
  • In this way, we rotate the given list to the right r times.
  • The Exit of the Program.

Below is the implementation:

# function which accepts the giveen list as argument
# and rotates the given list to the right by one time


def rightRotateByOne(given_list):
    # intializing last variable to the last element of the given list
    lastval = given_list[-1]
    # traversing length of given list -1 times using for loop
    for i in reversed(range(len(given_list) - 1)):
      # initializing the next index element to the current index element
        given_list[i+1] = given_list[i]
    # intializing first value to the last value of the given list
    given_list[0] = lastval


# function which accepts the given list and number
# of positions as arguments and rotate the given list r times here r=positions
def rightRotateList(given_list, positions):
    # Pass the given list to the rightRotateOne function inside the
    # for loop which rotates the list to the right one time.
    for numb in range(positions):
        rightRotateByOne(given_list)


# Driver Code
#Give the string list as user input using split(),list() functions.
given_list = list( input('Enter some random list separated by spaces = ').split())
print('The list before rotating r times = ', given_list)
# Give the number of rotations r as user input using int(input())
# and store it in another variable.
positions = int(input('Enter some random number of positions = '))
# Pass the given list and number of rotations as arguments to the rightRotateList function
# which rotates the given list to the right by r positions.
rightRotateList(given_list, positions)
# print the list after rotating to the right r times
print('The list after rotating r times : ', given_list)

Output:

Enter some random list separated by spaces = good morning this is btechgeeks
The list before rotating r times = ['good', 'morning', 'this', 'is', 'btechgeeks']
Enter some random number of positions = 3
The list after rotating r times : ['this', 'is', 'btechgeeks', 'good', 'morning']

Related Programs: