How to Remove the Last Item from a List
To remove the last item from a list, you can use the pop()
method, which removes and returns the last element of the list.
Example: Removing the Last Item
# Removing the last item from a list
fruits = ["apple", "banana", "cherry"]
last_fruit = fruits.pop() # Remove and return the last item
print(f"Removed item: {last_fruit}")
print(fruits)
Output
Removed item: cherry
["apple", "banana"]
Explanation: The pop()
method removes the last item from the list and returns it. After removing 'cherry', the list contains only 'apple' and 'banana'.