You can remove and get the last item from a Python list using various methods, some of which are as follows:

  1. Using list.pop();
  2. Slicing the List;
  3. Using del Statement;

#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]

Published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing.