Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to rotate a list in Python

In Python, you can rotate a list by using slicing or the collections.deque data structure. Here's how to do both:

  • Using slicing (for small to moderately-sized lists):

To rotate a list n positions to the left, you can use slicing to create a new list with the elements rearranged:

def rotate_list_left(lst, n):
    n = n % len(lst)  # Handle cases where n is greater than the length of the list
    return lst[n:] + lst[:n]

original_list = [1, 2, 3, 4, 5]
rotated_list = rotate_list_left(original_list, 2)
print(rotated_list)  # Output: [3, 4, 5, 1, 2]

To rotate a list n positions to the right, you can change the order of slicing:

def rotate_list_right(lst, n):
    n = n % len(lst)  # Handle cases where n is greater than the length of the list
    return lst[-n:] + lst[:-n]

original_list = [1, 2, 3, 4, 5]
rotated_list = rotate_list_right(original_list, 2)
print(rotated_list)  # Output: [4, 5, 1, 2, 3]
  • Using collections.deque (for larger lists or when frequent rotations are needed):

The deque data structure is a double-ended queue, allowing efficient insertion and removal of elements at both ends. To rotate a list using deque, you can use the rotate() method:

from collections import deque

def rotate_list(lst, n):
    d = deque(lst)
    d.rotate(n)  # Positive n for right rotation, negative n for left rotation
    return list(d)

original_list = [1, 2, 3, 4, 5]
rotated_list_left = rotate_list(original_list, -2)  # Left rotation
print(rotated_list_left)  # Output: [3, 4, 5, 1, 2]

rotated_list_right = rotate_list(original_list, 2)  # Right rotation
print(rotated_list_right)  # Output: [4, 5, 1, 2, 3]

In this example, the rotate_list() function takes a list and the number of positions to rotate. A positive n value rotates the list to the right, and a negative n value rotates the list to the left. The function converts the input list to a deque, performs the rotation, and then converts the deque back to a list.

  1. Circular shift list elements Python:

    • Description: Circularly shifting elements in a list to the right.
    • Code:
      def circular_shift_right(lst, positions):
          return lst[-positions:] + lst[:-positions]
      
      original_list = [1, 2, 3, 4, 5]
      shifted_list = circular_shift_right(original_list, 2)
      print("Original List:", original_list)
      print("Shifted List:", shifted_list)
      
  2. Shift elements in a list Python:

    • Description: Shifting elements in a list to the left.
    • Code:
      def shift_left(lst, positions):
          return lst[positions:] + lst[:positions]
      
      original_list = [1, 2, 3, 4, 5]
      shifted_list = shift_left(original_list, 2)
      print("Original List:", original_list)
      print("Shifted List:", shifted_list)
      
  3. Rotate list left/right in Python:

    • Description: Rotating elements in a list to the left or right.
    • Code:
      def rotate_list(lst, positions):
          return lst[-positions:] + lst[:-positions]
      
      original_list = [1, 2, 3, 4, 5]
      rotated_left = rotate_list(original_list, 2)
      rotated_right = rotate_list(original_list, -2)
      print("Original List:", original_list)
      print("Rotated Left:", rotated_left)
      print("Rotated Right:", rotated_right)
      
  4. Python list rotation example:

    • Description: Example of rotating elements in a list using slicing.
    • Code:
      original_list = [1, 2, 3, 4, 5]
      rotated_list = original_list[2:] + original_list[:2]
      print("Original List:", original_list)
      print("Rotated List:", rotated_list)
      
  5. Circular buffer in Python with list rotation:

    • Description: Implementing a circular buffer using list rotation.
    • Code:
      class CircularBuffer:
          def __init__(self, size):
              self.buffer = [None] * size
      
          def push(self, item):
              self.buffer = [item] + self.buffer[:-1]
      
          def get_buffer(self):
              return self.buffer
      
      circular_buffer = CircularBuffer(5)
      circular_buffer.push(1)
      circular_buffer.push(2)
      circular_buffer.push(3)
      print("Circular Buffer:", circular_buffer.get_buffer())
      
  6. Rotate elements of a list by N positions Python:

    • Description: Rotating elements of a list by a specified number of positions.
    • Code:
      def rotate_list_by_n(lst, n):
          return lst[-n:] + lst[:-n]
      
      original_list = [1, 2, 3, 4, 5]
      rotated_list = rotate_list_by_n(original_list, 3)
      print("Original List:", original_list)
      print("Rotated List:", rotated_list)
      
  7. Shift array elements in Python:

    • Description: Shifting array elements to the right.
    • Code:
      def shift_array_right(arr, positions):
          return arr[-positions:] + arr[:-positions]
      
      original_array = [1, 2, 3, 4, 5]
      shifted_array = shift_array_right(original_array, 2)
      print("Original Array:", original_array)
      print("Shifted Array:", shifted_array)
      
  8. List rotation algorithm in Python:

    • Description: Implementing a list rotation algorithm using slicing.
    • Code:
      def rotate_list_algorithm(lst, positions):
          return lst[-positions:] + lst[:-positions]
      
      original_list = [1, 2, 3, 4, 5]
      rotated_list = rotate_list_algorithm(original_list, 2)
      print("Original List:", original_list)
      print("Rotated List:", rotated_list)
      
  9. Rotate Python array or list elements:

    • Description: Rotating elements of a list using slicing.
    • Code:
      def rotate_elements(lst, positions):
          return lst[-positions:] + lst[:-positions]
      
      original_list = [1, 2, 3, 4, 5]
      rotated_list = rotate_elements(original_list, 2)
      print("Original List:", original_list)
      print("Rotated List:", rotated_list)