Open In App
Related Articles

Python | Shift last element to first position in list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate
# shift last element to first
# 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 last element to first
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 : [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

# Python3 code to demonstrate
# shift last element to first
# 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 last element to first
test_list.insert(0, test_list.pop())
 
# 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 : [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
 
# Initialize the list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
# Print the original list
print("The original list is:", test_list)
 
# Use islice to split the list into the last element and the rest of the list
last_element, rest_of_list = islice(test_list, len(test_list)-1, len(test_list)), islice(test_list, 0, len(test_list)-1)
 
# Concatenate the last element and the rest of the list to shift the last element to the front
test_list = list(last_element) + list(rest_of_list)
 
# Print the modified list
print("The modified list is:", 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 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

# Python3 code to demonstrate
# shift last element to first
 
# 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 last element to first
x=test_list[:len(test_list)-1]
y=test_list[len(test_list)-1:]
y.extend(x)
# printing result
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

# Python3 code to demonstrate
# shift last element to first
# importing deque
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))
 
 
# shift last element to first
my_deque = deque(test_list)
my_deque.rotate(1)
y = list(my_deque)
 
# printing result
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



Last Updated : 28 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads