python program to rotate a list n times
In the python programming tutorial, we are going to learn
-
python program to rotate a list
python program to rotate a list n times
The python program to rotate a list is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
my_list = [1,2,3,4,5,6,7,8,9]
n = int(input("Enter how many times you want to rotate the list: "))
new_list = my_list[n:] + my_list[:n]
print("After rotation:\n",new_list)
The output of the python list rotate is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter how many times you want to rotate the list: 5
After rotation:
[6, 7, 8, 9, 1, 2, 3, 4, 5]
Few important tips about the program
1. In the program, we have used the concept of slicing to generate the new list.
2. During slicing, it is to be noted that, the starting index is inclusive but the ending index is exclusive in nature.
python program to rotate a list:
This wraps up our session python program to rotate a list or python list rotate.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Jul 06,2022