0
0
PythonProgramBeginner · 2 min read

Python Program to Rotate Array Left

To rotate an array left by d positions in Python, use slicing like rotated = arr[d:] + arr[:d] where arr is your list.
📋

Examples

Input[1, 2, 3, 4, 5], d=2
Output[3, 4, 5, 1, 2]
Input[10, 20, 30, 40], d=1
Output[20, 30, 40, 10]
Input[7, 8, 9], d=3
Output[7, 8, 9]
🧠

How to Think About It

To rotate an array left by d positions, think of moving the first d elements to the end while keeping the order of the rest. You can split the array into two parts: from d to end, and from start to d, then join them in reverse order.
📐

Algorithm

1
Get the input array and the number of positions <code>d</code> to rotate.
2
Slice the array from index <code>d</code> to the end.
3
Slice the array from start to index <code>d</code>.
4
Concatenate the two slices to form the rotated array.
5
Return or print the rotated array.
💻

Code

python
def rotate_left(arr, d):
    d = d % len(arr)  # handle rotations greater than array length
    return arr[d:] + arr[:d]

# Example usage
array = [1, 2, 3, 4, 5]
rotated_array = rotate_left(array, 2)
print(rotated_array)
Output
[3, 4, 5, 1, 2]
🔍

Dry Run

Let's trace rotating [1, 2, 3, 4, 5] left by 2 positions through the code

1

Calculate effective rotation

d = 2 % 5 = 2

2

Slice from d to end

arr[2:] = [3, 4, 5]

3

Slice from start to d

arr[:2] = [1, 2]

4

Concatenate slices

[3, 4, 5] + [1, 2] = [3, 4, 5, 1, 2]

StepOperationResult
1Calculate d % len(arr)2
2Slice arr[d:][3, 4, 5]
3Slice arr[:d][1, 2]
4Concatenate slices[3, 4, 5, 1, 2]
💡

Why This Works

Step 1: Handle rotations larger than array length

Using d = d % len(arr) ensures the rotation count never exceeds the array size, avoiding unnecessary full rotations.

Step 2: Split the array into two parts

Slicing the array into arr[d:] and arr[:d] separates the part to move and the part to keep in front.

Step 3: Join slices to form rotated array

Concatenating arr[d:] + arr[:d] moves the first d elements to the end, completing the left rotation.

🔄

Alternative Approaches

Using deque from collections
python
from collections import deque

def rotate_left_deque(arr, d):
    dq = deque(arr)
    dq.rotate(-d)  # negative for left rotation
    return list(dq)

print(rotate_left_deque([1, 2, 3, 4, 5], 2))
Deque rotation is efficient for large arrays and supports in-place rotation but requires importing a module.
Rotate by repeated popping and appending
python
def rotate_left_pop(arr, d):
    for _ in range(d):
        first = arr.pop(0)
        arr.append(first)
    return arr

print(rotate_left_pop([1, 2, 3, 4, 5], 2))
This method is simple but inefficient for large arrays because popping from the front is slow.

Complexity: O(n) time, O(n) space

Time Complexity

Slicing and concatenation each take O(n) time because they process all elements once.

Space Complexity

The rotated array is a new list, so it uses O(n) extra space.

Which Approach is Fastest?

Slicing is fast and simple for small to medium arrays; deque rotation is efficient for large data and in-place rotation; repeated popping is slow and not recommended for big arrays.

ApproachTimeSpaceBest For
SlicingO(n)O(n)Simple and readable code
Deque rotationO(n)O(n)Efficient for large arrays, in-place rotation
Repeated poppingO(n*d)O(1)Small arrays or learning purposes only
💡
Use slicing with arr[d:] + arr[:d] for a clean and fast left rotation in Python.
⚠️
Forgetting to use modulo d % len(arr) can cause errors when d is larger than the array length.