The cyclic rotations have been discussed in the earlier articles. But sometimes, we just require a specific task, a part of rotation i.e shift first element to last element in list. This has the application in day-day programming in certain utilities. Let’s discuss certain ways in which this can be achieved.
Method #1 : Using list slicing and “+” operator
The combination of the these functions can be used to perform the task of single shift in list. The first element is added to the rest of the list to achieve this task using slicing.
# Python3 code to demonstrate # Shift from Front to Rear in List # using list slicing and "+" operator # initializing list test_list = [1, 4, 5, 6, 7, 8, 9, 12] # printing the original list print ("The original list is : " + str(test_list)) # using list slicing and "+" operator # Shift from Front to Rear in List test_list = test_list[1 :] + test_list[: 1] # printing result print ("The list after shift is : " + str(test_list)) |
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [4, 5, 6, 7, 8, 9, 12, 1]
Method #2 : Using insert() + pop()
This functionality can also be achieved using the inbuilt functions of python viz. insert() and pop(). The pop function removes the first element and that is inserted at last using the insert function.
# Python3 code to demonstrate # Shift from Front to Rear in List # using insert() + pop() # initializing list test_list = [1, 4, 5, 6, 7, 8, 9, 12] # printing the original list print ("The original list is : " + str(test_list)) # using insert() + pop() # Shift from Front to Rear in List test_list.insert(len(test_list) - 1, test_list.pop(0)) # printing result print ("The list after shift is : " + str(test_list)) |
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [4, 5, 6, 7, 8, 9, 12, 1]
Recommended Posts:
- Python | Front and rear range deletion in a list
- Python - Rear shift K in List
- Python | Append at front and remove from rear
- Access front and rear element of Python tuple
- Python | Strings with similar front and rear character
- Python | Alternate front - rear Sum
- Python | Retain K Front and Rear elements
- Python - Dictionary construction from front-rear key values
- Python - Strip front and rear Punctuations from given String
- Python - Remove rear element from list
- Python - Rear element extraction from list of tuples records
- Python - Test rear digit match in all list elements
- Python - Remove Rear K characters from String List
- Python - Rear elements Average in List
- Python - Concatenate Rear elements in Tuple List
- Python - Sort by Rear Character in Strings List
- Python | Replace rear word in String
- Python | Rear Addition of Record
- Python | Rear elements from Tuple Strings
- Python - Words with Particular Rear letter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

