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
In Python, you can rotate a list by using slicing or the collections.deque
data structure. Here's how to do both:
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]
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.
Circular shift list elements Python:
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)
Shift elements in a list Python:
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)
Rotate list left/right in Python:
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)
Python list rotation example:
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)
Circular buffer in Python with list rotation:
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())
Rotate elements of a list by N positions Python:
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)
Shift array elements in Python:
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)
List rotation algorithm in Python:
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)
Rotate Python array or list elements:
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)