Python Program for Array Rotation Last Updated : 13 Nov, 2025 Comments Improve Suggest changes 73 Likes Like Report Given an array of integers and a number d, the task is to rotate the array to the left by 'd' positions. In left rotation, each element moves one position to the left, and the first element moves to the end of the array.Example:Input: [1, 2, 3, 4, 5]d=2Output: [3, 4, 5, 1, 2]Using List SlicingThis method rotates an array by splitting it into two parts using slicing and then joining them in reverse order. Python arr = [1, 2, 3, 4, 5, 6] d = 2 arr[:] = arr[d:] + arr[:d] print(arr) Output[3, 4, 5, 6, 1, 2] Explanation:arr[d:]: elements from index d to end.arr[:d]: first d elements.arr[:] = arr[d:] + arr[:d]: updates array in-place.Using reverse() MethodThis method rotates an array by reversing the entire array and then reversing its parts separately. Python arr = [1, 2, 3, 4, 5, 6, 7, 8] d = 2 n = len(arr) arr.reverse() arr[:n-d] = arr[:n-d][::-1] arr[n-d:] = arr[n-d:][::-1] print(arr) Output[3, 4, 5, 6, 7, 8, 1, 2] Explanation:arr.reverse(): reverses the whole array.arr[:n-d][::-1] reverses the first part.arr[n-d:][::-1] reverses the last part.Using Temporary ArrayThis method rotates an array by storing the first 'd' elements in a temporary array, shifting the remaining elements left, and then appending the stored elements at the end. Python arr = [1, 2, 3, 4, 5, 6, 7] d = 2 n = len(arr) temp = arr[:d] arr[:n-d] = arr[d:] arr[n-d:] = temp print(arr) Output[3, 4, 5, 6, 7, 1, 2] Explanation:temp = arr[:d] store the first d elements.arr[:n-d] = arr[d:] shift the remaining elements left.arr[n-d:] = temp place the stored elements at the end.One by One Rotation Python arr = [1, 2, 3, 4, 5, 6, 7] d = 2 n = len(arr) for i in range(d): arr.append(arr.pop(0)) print(arr) Output[3, 4, 5, 6, 7, 1, 2] Explanation:arr.pop(0): removes the first element.arr.append(...): adds it to the end.Repeating d times rotates the array left by d positions.Related Articles:Juggling Algorithm for Array RotationRotate an Array by d - Counterclockwise or Left Create Quiz Comment K kartik Follow 73 Improve K kartik Follow 73 Improve Article Tags : Python Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like