Open In App

# Python | Shift from Front to Rear in List

The cyclic rotations have been discussed in the earlier articles. But sometimes, we just require a specific task, a part of rotation i.e shift first element to last element in list. This has the application in day-day programming in certain utilities. Let’s discuss certain ways in which this can be achieved.

Method #1: Using list slicing and “+” operator

The combination of these functions can be used to perform the task of a single shift in list. The first element is added to the rest of the list to achieve this task using slicing.

## Python3

 `# Python3 code to demonstrate``# Shift from Front to Rear in List``# using list slicing and "+" operator` `# initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# printing the original list``print``(``"The original list is : "` `+` `str``(test_list))` `# using list slicing and "+" operator``# Shift from Front to Rear in List``test_list ``=` `test_list[``1``:] ``+` `test_list[: ``1``]` `# printing result``print``(``"The list after shift is : "` `+` `str``(test_list))`

Output :

```The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [4, 5, 6, 7, 8, 9, 12, 1]```

Time Complexity: O(n) where n is the number of elements in the list “test_list”. The list slicing and “+” operator is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

Method #2: Using insert() + pop()

This functionality can also be achieved using the inbuilt functions of python viz. insert() and pop(). The pop function removes the first element and that is inserted at last using the insert function.

## Python3

 `# Python3 code to demonstrate``# Shift from Front to Rear in List``# using insert() + pop()` `# initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# printing the original list``print``(``"The original list is : "` `+` `str``(test_list))` `# using insert() + pop()``# Shift from Front to Rear in List``test_list.insert(``len``(test_list) ``-` `1``, test_list.pop(``0``))` `# printing result``print``(``"The list after shift is : "` `+` `str``(test_list))`

Output :

```The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [4, 5, 6, 7, 8, 9, 12, 1]```

Time complexity: O(1), since we are only inserting and popping elements from the list. The time it takes to do so does not depend on the size of the list.
bO(1), since we are only modifying the original list and not using any additional data structures.

Method #3 : Using pop() and extend() methods

## Python3

 `# Python3 code to demonstrate``# Shift from Front to Rear in List` `# initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# printing the original list``print``(``"The original list is : "` `+` `str``(test_list))` `# Shift from Front to Rear in List``a ``=` `test_list[``0``]``test_list.pop(``0``)``test_list.extend([a])` `# printing result``print``(``"The list after shift is : "` `+` `str``(test_list))`

Output

```The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [4, 5, 6, 7, 8, 9, 12, 1]```

Time Complexity: O(N)
Auxiliary Space: O(1)

Method #4: Using deque and rotate() from collections module

## Python3

 `# Python3 code to demonstrate``# Shift from Front to Rear in List``# using deque and rotate() from collections module` `# importing deque from collections module``from` `collections ``import` `deque` `# initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# printing the original list``print``(``"The original list is : "` `+` `str``(test_list))` `# using deque and rotate()``test_list ``=` `deque(test_list)``test_list.rotate(``-``1``)``test_list ``=` `list``(test_list)` `# printing result``print``(``"The list after shift is : "` `+` `str``(test_list))``# This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [4, 5, 6, 7, 8, 9, 12, 1]```

Time Complexity: O(N)
Auxiliary Space: O(N)

This approach uses the deque class from the collections module, which is a double-ended queue. It can be used for efficient appends and pops from both ends of the list. The rotate() function is used to rotate the elements in the deque, with a positive number indicating a rotation to the right, and a negative number indicating a rotation to the left. In this case, we rotate the elements by -1, which moves the first element to the last.

Method 5: Using a loop and temporary variables

This approach uses a loop to iterate through the list and shift each element one position to the left, with the first element becoming the last.

## Python3

 `# Python3 code to demonstrate``# Shift from Front to Rear in List``# using a loop and temporary variable` `# Initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# Printing the original list``print``(``"The original list is : "``, test_list)` `# Shift from Front to Rear in List using a loop and temporary variable``temp ``=` `test_list[``0``]``for` `i ``in` `range``(``len``(test_list)``-``1``):``    ``test_list[i] ``=` `test_list[i``+``1``]``test_list[``-``1``] ``=` `temp` `# Printing result``print``(``"The list after shift is : "``, test_list)`

Output

```The original list is :  [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is :  [4, 5, 6, 7, 8, 9, 12, 1]```

Time Complexity: O(N), where N is the length of given test_list
Auxiliary Space: O(1)

Method #6: Using a generator expression and list comprehension

1. Initialize the list
2. Create a generator expression to generate the elements of the list in a shifted order.
3. Use a list comprehension to generate the new list from the generator expression.
4. Print the new list.

## Python3

 `# Python3 code to demonstrate``# Shift from Front to Rear in List``# using a generator expression and list comprehension` `# Initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# Printing the original list``print``(``"The original list is : "``, test_list)` `# Shift from Front to Rear in List using a generator expression``# and list comprehension``shifted_list ``=` `[test_list[(i``+``1``) ``%` `len``(test_list)]``                ``for` `i ``in` `range``(``len``(test_list))]` `# Printing result``print``(``"The list after shift is : "``, shifted_list)`

Output

```The original list is :  [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is :  [4, 5, 6, 7, 8, 9, 12, 1]```

Time complexity: O(n)
Auxiliary space: O(n) (for the new list)

Method #7: Using numpy.roll() function

Steps:

Import numpy module

1. Create a numpy array from the given list using the array() function/
2. Use the roll() function of the numpy array to shift the elements from the front to the rear.
The roll() function takes two arguments –
the number of shifts and the axis along which to roll. Since we want to shift the elements along the first axis, we will pass 1 as the second argument to the roll() function.
3. Convert the resulting numpy array back to a list using the tolist() function.

## Python3

 `# Python code to shift elements from front to rear using numpy.roll()` `# Import numpy module``import` `numpy as np` `# Initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``12``]` `# Printing the original list``print``(``"The original list is: "``, test_list)` `# Converting list to numpy array``arr ``=` `np.array(test_list)` `# Shifting elements from front to rear``shifted_arr ``=` `np.roll(arr, ``-``1``, axis``=``0``)` `# Converting back to list``shifted_list ``=` `shifted_arr.tolist()` `# Printing the shifted list``print``(``"The list after shift is: "``, shifted_list)`

```OUTPUT :
The original list is:  [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is:  [4, 5, 6, 7, 8, 9, 12, 1]```

Time complexity: O(n), where n is the length of the list
Auxiliary space: O(n), as we are creating a numpy array of the same size as the original list.