In this blog post, I’ll show you three different methods how to remove last element from list in Python. You’ll also learn how to remove N last elements from a list.
What is a Python list?
Python list is a data structure used to store data in a specific order. A list is a sequence of items that can be accessed by an index. It is enclosed by square brackets and the items are separated by commas.
Remove last element from list in Python – 3 methods
The simplest way to remove last element from list in Python is to use the
pop() method. It accepts the optional
index argument, which is used to delete an element in that position. If no arguments are passed to the
pop() method, then the last element is removed from the list and returned as the result of a function call.
a = [10, 20, 'abc', 30] print(a) # [10, 20, 'abc', 30] a.pop() print(a) # [10, 20, 'abc']
WARNING - If the list is empty, pop() will raise the IndexError: pop from empty list.
2. Using slicing
Python lists can be sliced. Slicing a list means extracting part of that list based on a defined range. So if we extract everything except the last element of a list, that can serve as a deletion technique. Let’s see in the example how this works.
a = [10, 20, 'abc', 30] print(a) # [10, 20, 'abc', 30] a = a[:-1] print(a) # [10, 20, 'abc']
Remember that the index defined as -1 refers to the last element in a list. So, for example, python translates range
[:-1] as “range starting from index 0 to the last index, not including the last index”, or in our case, “range starting from index 0 to index 3, not including index 3”.
Another convenient method for removing the last element from the list in Python is using the
del keyword. It can come in handy when you need to get rid of any object in Python. The same applies to the elements of a list. All you have to do is type the
del keyword before the list element accessed via index. If index is -1,
del will remove the last element in a list.
a = [10, 20, 'abc', 30] print(a) # [10, 20, 'abc', 30] del a[-1] print(a) # [10, 20, 'abc']
Remove last N elements from list in Python
Let’s generalize previous methods a bit. What if you wanted to remove the last N elements from the list in Python? Don’t worry; the process is very similar.
1. Using slicing
Again, slicing can be used to remove the last N elements from the list. Only this time, instead of defining only the last index, you need to define the last N indexes. Let me show you how to do that.
In our example, we want to remove the last two elements from a list. We can store this value in a variable called N. When defining a range that will be sliced, instead of writing it as
[:-1], we write it as
[:-N]. This will create a new list from the existing one, containing all indexes except the last two (N).
a = [10, 20, 'abc', 30] print(a) # [10, 20, 'abc', 30] N = 2 a = a[:-N] print(a) # [10, 20]
Similar to the previous method, last N elements in list can be removed by using the
del keyword before a list slice you want to remove. List slice can be defined by specifying range that includes last N indexes.
a = [10, 20, 'abc', 30] print(a) # [10, 20, 'abc', 30] N = 2 del a[-N:] print(a) # [10, 20]
[-N:] into “range starting from index -N to the last index”, or in our example, “range starting from index 1 (not including index 1), to the last index”.