The cyclic rotations have been discussed in the earlier articles. In Python, sometimes we just require a specific task, a part of rotation i.e. shift the last element to first element in the list. This has the application in day-day programming in certain utilities. Let’s discuss certain ways in which this can be achieved.
Shift Last Element to First Position using list slicing
The combination of these functions can be used to perform the task of a single shift in a list. The last element is added to the rest of the list to achieve this task using List 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 : [12, 1, 4, 5, 6, 7, 8, 9]
Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n
Shift Last Element to First Position using Insert() + pop()
This functionality can also be achieved using the inbuilt functions of python viz. insert() and pop(). The pop function returns the last element and that is inserted at the Python front 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( 0 , test_list.pop())
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 : [12, 1, 4, 5, 6, 7, 8, 9]
Time complexity: O(1), as it performs a constant amount of work regardless of the size of the list.
Auxiliary space: O(1), as it only requires a constant amount of extra space to store the temporary variable used to store the last element of the list before inserting it at the first position.
Shift Last Element to First Position using islice()
The code first imports the slice function from the itertools module. It then initializes a list test_list with some values.Next, it uses islice () to split the list into two parts: the last element and the rest of the list. It does this by using islice to slice test_list with the start index as len(test_list)-1 and the stop index as len(test_list) for the first slice, and with the start index as 0 and the stop index as len(test_list)-1 for the second slice. Finally, it concatenates the two slices together using the + operator, resulting in the last element being moved to the front of the list. It then prints the modified list.
Python3
from itertools import islice
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is:" , test_list)
last_element, rest_of_list = islice(test_list, len (test_list) - 1 , len (test_list)), islice(test_list, 0 , len (test_list) - 1 )
test_list = list (last_element) + list (rest_of_list)
print ( "The modified list is:" , test_list)
|
Output
The original list is: [1, 4, 5, 6, 7, 8, 9, 12]
The modified list is: [12, 1, 4, 5, 6, 7, 8, 9]
Time complexity: O(n), where n is the number of elements in the list. This is because the time complexity of islice is O(n) and list concatenation has a time complexity of O(n).
Auxiliary space: O(n), as the function, creates two new lists from the original list, both with a size of n-1.
Shift Last Element to First Position using slicing+extend() Method
To shift the last element of a list to the first position using slicing and the extend()
method. Extract the last element of the list using slicing. Use the extend()
method to add the extracted element at the beginning of the list.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
x = test_list[: len (test_list) - 1 ]
y = test_list[ len (test_list) - 1 :]
y.extend(x)
print ( "The list after shift is : " + str (y))
|
Output
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]
Time Complexity : O(N)
Auxiliary Space : O(1)
Shift Last Element to First Position using Deque rotate() Method
To shift the last element of a list to the first position use the deque class from the collections module to create a deque object from the list and then use the rotate() method to rotate the deque by -1 positions.
Python3
from collections import deque
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
my_deque = deque(test_list)
my_deque.rotate( 1 )
y = list (my_deque)
print ( "The list after shift is : " + str (y))
|
Output
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]
Time Complexity: O(N) where n is the length of the list.
Space Complexity: O(N) as we need to create a deque object from the list, which requires O(n) space
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jul, 2023
Like Article
Save Article