Python Program to Rotate a List
To rotate a list in Python, use slicing like
rotated_list = original_list[n:] + original_list[:n] where n is the number of positions to rotate.Examples
Input[1, 2, 3, 4, 5], n=2
Output[3, 4, 5, 1, 2]
Input[10, 20, 30, 40], n=1
Output[20, 30, 40, 10]
Input[7, 8, 9], n=0
Output[7, 8, 9]
How to Think About It
To rotate a list, think of moving the first n elements to the end of the list. You can split the list into two parts: from the start to n, and from n to the end. Then join these parts in reverse order to get the rotated list.
Algorithm
1
Get the original list and the number of positions n to rotate.2
Split the list into two parts: first n elements and the rest.3
Concatenate the second part with the first part.4
Return or print the new rotated list.Code
python
def rotate_list(lst, n): n = n % len(lst) # handle rotation greater than list length return lst[n:] + lst[:n] # Example usage my_list = [1, 2, 3, 4, 5] rotated = rotate_list(my_list, 2) print(rotated)
Output
[3, 4, 5, 1, 2]
Dry Run
Let's trace rotating [1, 2, 3, 4, 5] by 2 positions through the code
1
Calculate effective rotation
n = 2 % 5 = 2
2
Split list into two parts
lst[n:] = [3, 4, 5], lst[:n] = [1, 2]
3
Concatenate parts
rotated_list = [3, 4, 5] + [1, 2] = [3, 4, 5, 1, 2]
| Step | List Part 1 (lst[n:]) | List Part 2 (lst[:n]) | Result |
|---|---|---|---|
| 1 | [3, 4, 5] | [1, 2] | [3, 4, 5, 1, 2] |
Why This Works
Step 1: Handle rotation greater than list length
Using n % len(lst) ensures the rotation number fits within the list size, avoiding extra full rotations.
Step 2: Split the list
The list is split into two slices: from n to end and from start to n.
Step 3: Concatenate slices
Joining the second slice with the first moves the first n elements to the end, achieving rotation.
Alternative Approaches
Using collections.deque rotate
python
from collections import deque def rotate_list_deque(lst, n): d = deque(lst) d.rotate(-n) # negative for left rotation return list(d) print(rotate_list_deque([1, 2, 3, 4, 5], 2))
Deque rotation is efficient for large lists and supports in-place rotation.
Using a loop to rotate
python
def rotate_list_loop(lst, n): n = n % len(lst) for _ in range(n): first = lst.pop(0) lst.append(first) return lst print(rotate_list_loop([1, 2, 3, 4, 5], 2))
This method is simple but less efficient for large lists due to repeated pop and append.
Complexity: O(n) time, O(n) space
Time Complexity
Slicing and concatenation each take O(n) time because they create new lists by copying elements.
Space Complexity
The rotated list is a new list of the same size, so space complexity is O(n).
Which Approach is Fastest?
Using collections.deque.rotate is faster for large lists because it rotates in place without copying.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Slicing | O(n) | O(n) | Simple and readable for small to medium lists |
| collections.deque.rotate | O(n) | O(n) | Efficient in-place rotation for large lists |
| Loop with pop and append | O(n^2) | O(1) | Easy to understand but slow for large lists |
Use slicing with modulo to handle rotations larger than the list length safely.
Forgetting to use modulo causes errors when rotation count exceeds list size.