In Python, we can remove the last element from a list easily – there are many ways to remove the last item from a list using Python.
The easiest way to remove the last element from a list is with slicing.
list = [1,2,9,0,1,3]
list_without_last_element = list[:-1]
print(list_without_last_element)
#Output:
[1,2,9,0,1]
Another method to remove the last item in a list is with the Python pop() function.
list = [1,2,9,0,1,3]
print(list.pop())
#Output:
[1,2,9,0,1]
You can also use the Python del keyword to delete the last element from a list.
list = [1,2,9,0,1,3]
del list[-1]
print(list)
#Output:
[1,2,9,0,1]
Finally, you can use the deque() object from the Python collections module to get rid of the last element in a list.
from collections import deque
list = [1,2,9,0,1,3]
dequelist = deque(list)
dequelist.pop()
print(list(dequelist))
#Output:
[1,2,9,0,1]
In Python, lists are one of the most used data structures and allow us to work with collections of data easily. When working with lists, it is useful to be able to add or remove items from the list in an easy way.
With Python, we can easily remove the last element from a list. There are multiple ways to remove the last item, and the easiest way to get rid of the last item of a list is with slicing.
To use slicing to remove the last element in a list, we don’t provide a start position and then specify ‘-1’ as the end position to get everything except the last element.
Below is an example of how we can use slicing in Python to get rid of the last element of a list.
list = [1,2,9,0,1,3]
list_without_last_element = list[:-1]
print(list_without_last_element)
#Output:
[1,2,9,0,1]
If you are looking to remove the first element from a list, you can also use slicing, or any of these methods which you will see below.
Using the pop Function in Python to Remove the Last Item from a List
There are many list methods which allow us to manipulate lists in Python very easily. One such method is the Python pop() function.
The Python pop() function removes an element at the specified position. If no position is specified, then pop() removes the last element in the list.
Below is an example of how to use the pop() function to remove the last element from a list in Python.
list = [1,2,9,0,1,3]
print(list.pop())
#Output:
[1,2,9,0,1]
Using the del Keyword in Python to Remove the Last Element in a List
The Python del keyword allows us to delete objects. We can use the del to delete the last element of a list.
Below is an example in Python of how to use del to delete the last element of a list.
list = [1,2,9,0,1,3]
del list[-1]
print(list)
#Output:
[1,2,9,0,1]
Using deque in Python to Remove the Last Item from a List
The last way that you can remove the last item from a list is with the deque data structure from the Python collections module.
Deque, or doubly ended queue, is most useful if you need to quickly append or pop items from the beginning or end of your data. If you have a large collection of items, you deque can be faster than the similar list operations.
To remove the last item from a list using deque, we convert the list to deque, use the pop() function, and then convert the result back to a list.
from collections import deque
list = [1,2,9,0,1,3]
dequelist = deque(list)
dequelist.pop()
print(list(dequelist))
#Output:
[1,2,9,0,1]
Hopefully this article has been beneficial for you to learn the different ways you can remove the last element in a list using Python.