What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Remove Last Element from List in Python

  • Feb 14, 2022
  • 5 Minutes Read
Remove Last Element from List in Python

Insertion and Deletion are some of the most important operations in any Data Structure. In this article, we are going to focus on the deletion operation of a list. To be more precise we are going to learn how to delete the last element of a list in python. If you don’t know what a list is, you might want to check out our article on deleting the first element in a list.

When it comes to quick storing and manipulating data while programming, Python lists are one of the most commonly utilized sequential data structures. Lists are a type of collection that greatly simplifies the lives of programmers. Removing the last element from a list excludes the last element of a list by shortening the list. For example, if the given list is [1, 2, 3, 4], then the list after the removal of the last element is [1,2,3].  This removal operation reduces the length of the list by one. Let us now look at the methods using which we can remove the last element of a list in Python.

How to Delete the Last Element of a List in Python?

Python provides us with an array of methods, using which we can achieve our target. In this section, we will discuss each of these methods one by one.

1. Using List.pop() method

The List library contains a large number of methods, but the one which we will be using is a method called List.pop(). This method takes one argument, index. The element at the specified position is deleted by this method. However, this argument is optional and if no index is specified then the last element of the list is deleted. The method returns the element which is popped. Note that the result of this operation is inplace, i.e the change takes place in the list itself.

The pop function raises an IndexError, if we try to pop from an empty list.

For example:

# program to delete the last
# last element from the list 
#using pop method

list = [1,2,3,4,5]
print("Original list: " +str(list))

# call the pop function
# ele stores the element 
#popped (5 in this case)
ele= list.pop()

# print the updated list
print("Updated list: " +str(list))

 

Output:

Original list: [1, 2, 3, 4, 5]
Updated list: [1, 2, 3, 4]

 

2. Using Slicing

Python provides a provision for slicing the lists using the subscript operator. For slicing a list, one needs to provide the starting and ending index in the subscript operator.

list[start: end]

 

The end index is exclusive, i.e. the sublist generated by this method includes elements from the start index until the end-1 index. If the start index is not provided then the elements are selected from the first index of the list. And if no end index is given, then it selects elements until the last index of the list.

We can also provide a negative index, however, in this case, the counting of the indexes starts from the last element. Allow us to elaborate on the last point, the index of the last element of a list is -1, and that of the second last element is -2, and so on. So in order to delete the last element from a list, elements need to be selected from index 0 to -1.

For example:

# program to delete the last
# last element from the list 
#using slicing

list = [1,2,3,4,5]
print("Original list: " +str(list))

# slice the list 
# from index 0 to -1
list = list[ : -1]

# print the updated list
print("Updated list: " +str(list))

 

Output:

Original list: [1, 2, 3, 4, 5]
Updated list: [1, 2, 3, 4]

 

This approach, however, has a major drawback, it generates a copy of the sliced list which needs to be stored into a list variable. Due to this reason, we don’t recommend you to use this approach. This method has an advantage as well, this method does not throw an error when applied to an empty list.

3. Using del

Another efficient, yet simple approach to delete the last element of the list is by using the del statement. The del operator deletes the element at the specified index location from the list. To delete the last element, we can use the negative index  -1. The use of the negative index allows us to delete the last element, even without calculating the length of the list. This decreases the complexity of the program significantly.

You may be wondering how does this approach differ from the List.pop() method? Both the mentioned approaches have one significant difference and similarity between them. Unlike the pop method, the del operator does not return the deleted element. But like the pop method, it raises an IndexError when we try to delete an element from an empty list or specify an index that is greater than the length of the list.

In contrast with the slicing approach, this method does not return the sliced list. This means that the changes are inplace in this approach.

For example:

# program to delete the last
# last element from the list 
#using del

list = [1,2,3,4,5]
print("Original list: " +str(list))

# call del operator
del list[-1]

# print the updated list
print("Updated list: " +str(list))

 

Output:

Original list: [1, 2, 3, 4, 5]
Updated list: [1, 2, 3, 4]

 

Conclusion

Python is known for its easy syntax and English-like commands. Having a strong grasp on the insertion and deletion of operations is a skill that every programmer should possess. In this article, we discussed three approaches, along with their pros and cons, using which we can delete the last element of a list. However, the pop() function is the most recommended and widely accepted method to delete an element from the list.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Riddhima Agarwal
Hey, I am Riddhima Agarwal, a B.tech computer science student and a part-time technical content writer. I have a passion for technology, but more importantly, I love learning. Looking forward to greater opportunities in life. Through my content, I want to enrich curious minds and help them through their coding journey