# Program to cyclically rotate an array by one in Python | List Slicing

Last Updated : 16 Nov, 2023

Given an array, cyclically rotate the array clockwise by one. In this article, we will see how to cyclically rotate an array by one in Python.

Example

`Input:  arr = [1, 2, 3, 4, 5]Output: arr = [5, 1, 2, 3, 4]`

## Python Program to Cyclically Rotate an Array by One in Python

Below are the methods or the way by which we can cyclically rotate an array by one in Python:

• Using List Slicing
• Using Temporary Variable

### Python Cyclically Rotate an Array by One Using List Slicing

We will solve this problem in Python quickly using List Slicing. The approach is very simple, just remove the last element in the list and append it in front of the remaining list.

## Python3

 `# Program to cyclically rotate an array by one`   `def` `cyclicRotate(``input``):` `    ``print``([``input``[``-``1``]] ``+` `input``[``0``:``-``1``])`   `# Driver program` `if` `__name__ ``=``=` `"__main__"``:` `    ``input` `=` `[``1``, ``2``, ``3``, ``4``, ``5``]` `    ``cyclicRotate(``input``)`

Output

```[5, 1, 2, 3, 4]

```

Time Complexity: O(n)
Space Complexity:O(1)

### Python Program to Cyclically Rotate an Array Using temporary variable

This approach uses a for loop and temporary variable to cyclically rotate the given array by one position. Below is the code implementation of the following method.

## Python3

 `arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``]` `n ``=` `len``(arr)`   `# store last element in a temporary variable` `temp ``=` `arr[n``-``1``]`   `# shift each element one position to the right` `for` `i ``in` `range``(n``-``1``, ``0``, ``-``1``):` `    ``arr[i] ``=` `arr[i``-``1``]`   `# move the temporary variable to the first position` `arr[``0``] ``=` `temp` `print``(arr)`

Output

```[5, 1, 2, 3, 4]

```

Time Complexity: O(n)
Space Complexity: O(1)