How to delete last item in list?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, deleting the last item from a list is a common operation for stack-like workflows, incremental parsing, and cleanup logic. The best method depends on whether you want the removed value back, whether you need to modify the original list in place, and how you want to handle empty lists. Python gives you a few clean choices once you understand those tradeoffs.
Use pop() when you want the removed value
The most common tool is list.pop(). With no argument, it removes and returns the last item.
This prints 40, and the list becomes [10, 20, 30]. pop() is ideal when the list is acting like a stack because it both removes the element and gives it back to you.
You can also write logic around the returned value:
Use del when you only want removal
If you do not need the removed element, del is a direct option.
This modifies the list in place and does not return anything. It is simple and explicit when the only goal is deletion.
Use slicing when you want a new list
Sometimes you want to keep the original list unchanged and create a new list without the last element. Slicing is the right tool for that.
This leaves original untouched and creates a new list containing [1, 2, 3]. That is useful in functional-style code or when multiple variables should not share the same mutable object.
Handling empty lists safely
Both pop() and del items[-1] raise an IndexError if the list is empty. If empty lists are possible, guard the operation.
This pattern keeps the code clear and avoids exception-driven control flow for a very common case.
If exceptions are already part of the calling pattern, you can handle them explicitly:
Choosing the right method
Use pop() when:
- You need the removed value
- The list behaves like a stack
- In-place mutation is acceptable
Use del when:
- You want to mutate in place
- You do not need the removed value
Use slicing when:
- You want a new list
- The original list should stay unchanged
These methods are all valid. The right one depends more on intent than on performance in most everyday code.
Common Pitfalls
The biggest mistake is calling pop() on an empty list without checking first. That raises IndexError, which is easy to avoid if emptiness is a normal possibility.
Another issue is assuming slicing changes the original list. It does not. items[:-1] creates a new list and leaves items as it was unless you assign the result back.
Developers also sometimes use remove() by mistake. remove() deletes by value, not by position, so it is not the right way to delete the last item unless you already know the last value and want to remove its first matching occurrence.
Finally, be aware of shared references. If two variables point to the same list object, pop() and del affect both references because they mutate the list in place.
Summary
- Use
pop()to remove and return the last list item. - Use
del items[-1]when you only want in-place deletion. - Use
items[:-1]when you want a new list without changing the original. - Guard against empty lists before removing the last element.
- Do not confuse
remove()by value with deletion by position.

