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
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
test_list = test_list[ 1 :] + test_list[: 1 ]
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
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
test_list.insert( len (test_list) - 1 , test_list.pop( 0 ))
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
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
a = test_list[ 0 ]
test_list.pop( 0 )
test_list.extend([a])
print ( "The list after shift is : " + str (test_list))
|
OutputThe 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
from collections import deque
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
test_list = deque(test_list)
test_list.rotate( - 1 )
test_list = list (test_list)
print ( "The list after shift is : " + str (test_list))
|
OutputThe 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
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " , test_list)
temp = test_list[ 0 ]
for i in range ( len (test_list) - 1 ):
test_list[i] = test_list[i + 1 ]
test_list[ - 1 ] = temp
print ( "The list after shift is : " , test_list)
|
OutputThe 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
- Initialize the list
- Create a generator expression to generate the elements of the list in a shifted order.
- Use a list comprehension to generate the new list from the generator expression.
- Print the new list.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " , test_list)
shifted_list = [test_list[(i + 1 ) % len (test_list)]
for i in range ( len (test_list))]
print ( "The list after shift is : " , shifted_list)
|
OutputThe 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
- Create a numpy array from the given list using the array() function/
- 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. - Convert the resulting numpy array back to a list using the tolist() function.
Python3
import numpy as np
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is: " , test_list)
arr = np.array(test_list)
shifted_arr = np.roll(arr, - 1 , axis = 0 )
shifted_list = shifted_arr.tolist()
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.