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.

</>
Copy
# 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:

  1. numbers[-k:]: Extracts the last k elements.
  2. numbers[:-k]: Extracts the elements before the last k elements.
  3. 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.

</>
Copy
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:

  1. We convert the list into a deque object.
  2. The rotate() method shifts elements by k positions.
  3. A positive k rotates to the right, while a negative k rotates to the left.
  4. 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.

</>
Copy
# 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:

  1. We loop k times to perform the rotation.
  2. Each iteration removes the last element using pop().
  3. 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.

</>
Copy
# 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:

  1. The new index of an element is determined using (i - k) % len(numbers).
  2. This formula ensures a cyclic shift.
  3. 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:

  1. Using slicing: The easiest and most efficient way.
  2. Using collections.deque: Best for performance in large lists.
  3. Using pop() and insert(): Suitable for small lists.
  4. Using list comprehension: A mathematical approach for rotation.