You can remove and get the last item from a Python list using various methods, some of which are as follows:
#Using list.pop()
The list.pop() method removes and returns the last item from a list, as shown in the following example:
my_list = [1, 2, 3, 4, 5]
last_item = my_list.pop()
print(last_item) # 5
print(my_list) # [1, 2, 3, 4]
#Slicing the List
You can use negative indexing to access the last item of a list and then remove it using slicing, like so:
my_list = [1, 2, 3, 4, 5]
last_item = my_list[-1]
my_list = my_list[:-1]
print(last_item) # 5
print(my_list) # [1, 2, 3, 4]
#Using del Statement
You can use the del statement to remove the last item from a list, as demonstrated below:
my_list = [1, 2, 3, 4, 5]
last_item = my_list[-1]
del my_list[-1]
print(last_item) # 5
print(my_list) # [1, 2, 3, 4]