Program to cyclically rotate an array by one in Python | List Slicing
Given an array, cyclically rotate the array clockwise by one.
Examples:
Input: arr = [1, 2, 3, 4, 5] Output: arr = [5, 1, 2, 3, 4]
We have existing solution for this problem please refer Program to cyclically rotate an array by one link. We will solve this problem in python quickly using List Comprehension. Approach is very simple, just remove last element in list and append it in front of remaining list.
# Program to cyclically rotate an array by one def cyclicRotate(input): # slice list in two parts and append # last element in front of the sliced list # [input[-1]] --> converts last element pf array into list # to append in front of sliced list # input[0:-1] --> list of elements except last element print ([input[-1]] + input[0:-1]) # Driver program if __name__ == "__main__": input = [1, 2, 3, 4, 5] cyclicRotate(input) |
chevron_right
filter_none
Output:
[5, 1, 2, 3, 4]
Recommended Posts:
- Python program to right rotate a list by n
- String slicing in Python to rotate a string
- Python | Custom slicing in List
- Python | Variable list slicing
- Python List Comprehension and Slicing
- Python | Alternate range slicing in list
- Python | Get the substring from given string using list slicing
- Python Slicing | Reverse an array in groups of given size
- Python | Ways to rotate a list
- Python | Slicing list from Kth element to last element
- Python | Reverse Slicing of given string
- Interesting facts about strings in Python | Set 2 (Slicing)
- Python Slicing | Extract ‘k’ bits from a given position
- Basic Slicing and Advanced Indexing in NumPy Python
- Python program to create a list of tuples from given list having number and its cube in each tuple
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.
Improved By : KarlKwon



