Rotate a List in Python
To rotate a list in Python, we can use slicing, the collections.deque
module, the pop()
and insert()
methods, or list comprehensions. Rotation involves shifting elements to the left or right while maintaining the order of elements in a cyclic manner. Let’s explore different ways to achieve this with examples.
Examples
1. Rotating a List Using Slicing
List slicing allows us to easily rotate a list by extracting two parts and swapping their positions.
# Define the list
numbers = [1, 2, 3, 4, 5]
# Number of positions to rotate (right)
k = 2
# Rotating the list using slicing
rotated_list = numbers[-k:] + numbers[:-k]
# Printing the rotated list
print("Rotated List:", rotated_list)
Explanation:
We use slicing to extract two parts of the list:
numbers[-k:]
: Extracts the lastk
elements.numbers[:-k]
: Extracts the elements before the lastk
elements.- By concatenating these two parts, we achieve a right rotation of the list.
Output:
Rotated List: [4, 5, 1, 2, 3]

2. Rotating a List Using collections.deque
The collections.deque
module provides a built-in method to rotate a list efficiently.
from collections import deque
# Define the list
numbers = [1, 2, 3, 4, 5]
# Convert list to deque
deque_list = deque(numbers)
# Number of positions to rotate (left)
k = 2
# Rotate using deque
deque_list.rotate(-k)
# Convert deque back to list
rotated_list = list(deque_list)
# Printing the rotated list
print("Rotated List:", rotated_list)
Explanation:
- We convert the list into a
deque
object. - The
rotate()
method shifts elements byk
positions. - A positive
k
rotates to the right, while a negativek
rotates to the left. - Finally, we convert the
deque
back into a list using list().
Output:
Rotated List: [3, 4, 5, 1, 2]

3. Rotating a List Using pop()
and insert()
We can rotate a list by repeatedly removing the last element and inserting it at the beginning.
# Define the list
numbers = [1, 2, 3, 4, 5]
# Number of positions to rotate (right)
k = 2
# Rotating using pop and insert
for _ in range(k):
last_element = numbers.pop() # Remove the last element
numbers.insert(0, last_element) # Insert at the beginning
# Printing the rotated list
print("Rotated List:", numbers)
Explanation:
- We loop
k
times to perform the rotation. - Each iteration removes the last element using
pop()
. - The removed element is then inserted at index 0 using
insert(0, element)
.
Output:
Rotated List: [4, 5, 1, 2, 3]

4. Rotating a List Using List Comprehension
We can use list comprehension along with the modulus operator to achieve rotation.
# Define the list
numbers = [1, 2, 3, 4, 5]
# Number of positions to rotate (right)
k = 2
# Rotating using list comprehension
rotated_list = [numbers[(i - k) % len(numbers)] for i in range(len(numbers))]
# Printing the rotated list
print("Rotated List:", rotated_list)
Explanation:
- The new index of an element is determined using
(i - k) % len(numbers)
. - This formula ensures a cyclic shift.
- The result is stored in a new list using list comprehension.
Output:
Rotated List: [4, 5, 1, 2, 3]

Conclusion
In this tutorial, we explored multiple ways to rotate a list in Python:
- Using slicing: The easiest and most efficient way.
- Using
collections.deque
: Best for performance in large lists. - Using
pop()
andinsert()
: Suitable for small lists. - Using list comprehension: A mathematical approach for rotation.