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 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 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 in a list variable. Due to this reason, we don’t recommend you use this approach. This method has an advantage as well, this method does not throw an error when applied to an empty list.
We know that these concepts are not easy to grasp in one go. Maybe our python tutors can teach you to teach you with 1-on-1 live classes with full focus on your doubts.
3. Using del statement
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 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]
4. Using List Comprehension
Another elegant approach to removing the last element from a list in Python is to use a technique called "list comprehension." List comprehension allows you to create a new list based on an existing one while applying a condition.
Here's how you can use list comprehension to accomplish this task:
Suppose you have a list like this: [1, 2, 3, 4, 5], and you want to delete the last element (5). With list comprehension, you can create a new list that excludes the last element.
For example:
# Example: Deleting the last element using list comprehension my_list = [1, 2, 3, 4, 5] print("Original list: " + str(my_list)) # Use list comprehension to create a new list without the last element new_list = [x for x in my_list if x != my_list[-1]] # Print the updated list print("Updated list: " + str(new_list))
Output:
Original list: [1, 2, 3, 4, 5] Updated list: [1, 2, 3, 4]
In this example, list comprehension provides a concise and readable way to create a new list that excludes the last element. It iterates through each element in the original list (my_list) and includes it in the new list only if it's not equal to the last element of the original list. This approach allows you to remove the last element while preserving the original list intact.
But if you want to delete a variable completely in python, you can do it with the del() method.
Additional Tips and Tricks
Here are some additional tips that you should keep in mind while working with list element deletion in Python:
1. Checking for an Empty List
- Before performing any deletion operation, consider checking if the list is empty.
- Use a conditional statement to ensure there are elements in the list.
- This check helps prevent errors when dealing with empty lists.
2. Preserving the Original List
- When using pop() or the del statement, remember that these operations modify the original list in place.
- If you need to keep the original list intact while working with a modified version, make a copy of the list.
- You can create a copy of a list using methods such as list(), list slicing, or the copy() method.
- Preserving the original list is useful when you require both the original and modified versions for different purposes.
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.