Shift Last Element to First Position in list - Python
Last Updated :
05 Feb, 2025
The task of shifting the last element to the first position in a list in Python involves modifying the order of elements such that the last item of the list is moved to the beginning while the rest of the list remains intact. For example, given a list a = [1, 4, 5, 6, 7, 8, 9, 12], the goal is to shift the last element, 12, to the first position, resulting in the list [12, 1, 4, 5, 6, 7, 8, 9].
Using list slicing
List slicing is one of the most efficient way to shift elements in a list. It allows us to create sublists with minimal overhead and perform the operation concisely in a single line.
Python
a = [1, 4, 5, 6, 7, 8, 9, 12]
a = a[-1:] + a[:-1]
print (str(a))
Output[12, 1, 4, 5, 6, 7, 8, 9]
Explanation: a[-1:] + a[:-1] shifts the last element of the list to the front by concatenating the last element (a[-1:]) with the rest of the list (a[:-1]).
Using deque
deque from collections module is specifically optimized for operations at both ends of a list. It's a great choice for situations where frequent modifications to the ends of the list are required.
Python
from collections import deque
a = [1, 4, 5, 6, 7, 8, 9, 12]
dq = deque(a) # Convert the list into a deque
dq.rotate(1)
a = list(dq)
print(a)
Output[12, 1, 4, 5, 6, 7, 8, 9]
Explanation: dq.rotate(1) rotates the deque by one position, moving the last element to the front.
Using insert
insert() and pop() methods offer a more manual but still efficient way to shift elements in a list. This method removes the last element using pop() and inserts it at the start using insert().
Python
a = [1, 4, 5, 6, 7, 8, 9, 12]
a.insert(0, a.pop())
print(a)
Output[12, 1, 4, 5, 6, 7, 8, 9]
Explanation:a.insert(0, a.pop()) inserts the popped element 12 at the beginning of the list, shifting the rest of the elements one position to the right.
Using list comprehension
List comprehension provides a concise and elegant way to create a new list, which is useful for performing operations like shifting elements. We can use it to prepend the last element and append the rest of the list.
Python
a = [1, 4, 5, 6, 7, 8, 9, 12]
a = [a[-1]] + [x for x in a[:-1]]
print(a)
Output[12, 1, 4, 5, 6, 7, 8, 9]
Explanation:[x for x in a[:-1]] creates a new list by iterating over all elements of a except the last one, effectively generating a copy of the list without its last element.
Similar Reads
Get first and last elements of a list in Python The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way
3 min read
Python program to insert an element into sorted list Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function
2 min read
Sorting List of Lists with First Element of Each Sub-List in Python In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list o
3 min read
Python program to interchange first and last elements in a list Given a list, write a Python program to swap the first and last element of the list using Python.Examples: The last element of the list can be referred to as a list[-1]. Therefore, we can simply swap list[0] with list[-1].Python# Initialize a list my_list = [1, 2, 3, 4, 5] # Interchange first and la
5 min read
Python Program to Swap Two Elements in a List In this article, we will explore various methods to swap two elements in a list in Python. The simplest way to do is by using multiple assignment.Example:Pythona = [10, 20, 30, 40, 50] # Swapping elements at index 0 and 4 # using multiple assignment a[0], a[4] = a[4], a[0] print(a)Output[50, 20, 30,
1 min read