Hey Folks! Today in this tutorial, we will learn how one can rotate a list using the python programming language. List rotation is a simple method that comes in helpful for programmers. Let’s go over various options for achieving rotation.
Rotate Lists in Python
Let’s understand how you can rotate lists in Python now. We’ll look at the multiple different methods of doing so below.
Method 1 : Slicing Operation
Another method for rotating a list is slicing. The
len() method is used to do this. The list is sliced in the following example.
In this situation, the value of
n_splits is 1, indicating the number of slices. As a result, the list is cycled in the same manner.
# Define the list list_1 = [1,2,3,4,5] print("Original list:", list_1) # Set the number of splits n_splits = 1 # Rotate The List list_1 = (list_1[len(list_1) - n_splits:len(list_1)] + list_1[0:len(list_1) - n_splits]) print("Rotated list:", list_1)
Original list: [1, 2, 3, 4, 5] Rotated list: [5, 1, 2, 3, 4]
Method 2: Traversing Operation
This is the simplest way to rotate a list in Python. We go through the list one by one, as the name implies. The element is then placed in its proper position.
This strategy is demonstrated in the example below. In this situation, we rotate the list by an integer n, which is 1.
def ROTATE (lists, n): output_list =  x= len(lists) for item in range(x - n, x): output_list.append(lists[item]) for item in range(0, x - n): output_list.append(lists[item]) return output_list rotate_num = 1 list_1 = [1, 2, 3, 4, 5] print("Original List:", list_1) print("Rotated list: ",ROTATE(list_1, rotate_num))
Original List: [1, 2, 3, 4, 5] Rotated list: [5, 1, 2, 3, 4]
Method 3: List comprehensions
We modify the indexing of a list in this approach by reassigning a new index to each element after rotation. In the following example, the list is rotated by one time, and new index values are assigned.
list_1 = [1, 2, 3, 4, 5] print ("Original List : " + str(list_1)) list_1 = [list_1[(i + 4) % len(list_1)] for i, x in enumerate(list_1)] print ("Rotated list : " + str(list_1))
Original List : [1, 2, 3, 4, 5] Rotated list : [5, 1, 2, 3, 4]
Method 4: Using collections Module
There is a
collection module in Python that has a
deque class. This class includes a rotate() method.
In the following example, we utilized the built-in function
from collections import deque list_1 = [1, 2, 3, 4, 5] print ("Original List : " + str(list_1)) list_1 = deque(list_1) list_1.rotate(-4) list_1 = list(list_1) print ("Rotated List: " + str(list_1))
Original List : [1, 2, 3, 4, 5] Rotated List: [5, 1, 2, 3, 4]
Congratulations! You just learned how to perform rotation on a list using multiple methods. Hope you enjoyed it! 😇
Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:
- 5 Easy ways to convert a dictionary to a list in Python
- How to convert lists to dataframes in Python?
- How to convert a list to a dictionary in Python?
- 3 Easy Methods to Print a Python List
Thank you for taking your time out! Hope you learned something new!! 😄