Python Program to Remove the Last Element from a List

In this article, we will show how to write a Python program to remove the last element or item from a given list with examples. There are multiple approaches to remove the last element from a list, and we show all the possibilities.

Using del statement

In Python, we can use the negative index positions to access the list items from right to left. In the program below, we have created a simple integer list.

As we know, del will remove let itemizing their index position. In the below python program, we pass the -1 or len(a) – 1 as the del function index number because it represents the last element in a list, and it will remove the same.

a = [15, 30, 45, 60, 75, 90]
print(a)

del a[-1]
print(a)

del a[len(a) - 1]
print(a)
[15, 30, 45, 60, 75, 90]
[15, 30, 45, 60, 75]
[15, 30, 45, 60]

Python Program to Remove the Last Element from a List using pop()

In general, the pop() method uses an index and removes the list item. However, by default, if you don’t pass any argument, it will remove the last item from a list. So, the a.pop() statement will remove the last element from the given list.

a = [15, 30, 45, 60, 75, 90]
print(a)

a.pop()
print(a)
[15, 30, 45, 60, 75, 90]
[15, 30, 45, 60, 75]

Using list slicing

The list slicing will slice the existing list by removing the given start and end points. In the below program, (a = a[:-1]) means we are slicing the existing list by omitting (removing) the last element and assigning it to a list.

a = [15, 30, 45, 60, 75, 90]
print(a)

a = a[:-1]
print(a)
[15, 30, 45, 60, 75, 90]
[15, 30, 45, 60, 75]
Python Program to Remove the Last element from a List

Using remove() method

The remove() function removes the given list element, and the below example passes “Germany” as the argument because it is the last one, and it removes that list item.

countries = ["India", "USA", "UK", "France", "Germany"]
print(countries)

countries.remove("Germany")
print(countries)

countries.remove("France")
print(countries)
['India', 'USA', 'UK', 'France', 'Germany']
['India', 'USA', 'UK', 'France']
['India', 'USA', 'UK']

Python Program to Remove the Last Element from a List Using List Comprehension

In the program below, we use list comprehension to remove the last element from a given list. Here, the list comprehension will create a new list starting from 0 to n-1, where n is the list length. In the code below, we have commented two lines because all three lines return the same result, and you can use any one of them.

a = [15, 30, 50, 60, 75]
print(a)

a = [n for n in a[:-1]]
#a = [n for n in a[0:-1]]
#a = [n for n in a[0:len(a)-1]]

print(a)
[15, 30, 50, 60, 75]
[15, 30, 50, 60]

Using List Unpack 

In the program below, first, unpack the list items using the * operator. Next, (*a, _ = a) will remove the last element from a given list. Similarly, (*a, _, _ = a) statement removes the last two items.

a = [15, 30, 40, 50, 60]
print(a)
print(*a)

*a, _ = a
print(a)

*a, _, _ = a
print(a)
[15, 30, 40, 50, 60]
15 30 40 50 60
[15, 30, 40, 50]
[15, 30]