How to Remove the Last Element from a List in Python

To remove the last element from a list in Python, call my_list.pop(). It deletes the final item and hands it back to you. If you don’t need the value, del my_list[-1] does the same job:

last = cart.pop()     # removes and returns the last item
del cart[-1]          # removes it, returns nothing

The outputs below came straight from Python 3.12.5, using a shopping cart and an undo history as examples.

We’ll go through pop(), del and slicing, removing the last few items, empty lists, and which approach is fastest.

Remove the last element with pop()

pop() with no argument works from the end, which makes a list a natural stack. Here it undoes the most recent actions:

history = ["typed 'Austin'", "added $19.99 item", "applied coupon"]

last = history.pop()                    # removes AND returns the last item
print("undo:", last)
print("left:", history)

print("undo:", history.pop(), "| left:", history)

Output:

undo: applied coupon
left: ["typed 'Austin'", 'added $19.99 item']
undo: added $19.99 item | left: ["typed 'Austin'"]
Command Prompt output of Python list.pop removing and returning the last two actions from an undo history list
Each pop() returns the newest action and shortens the list.

Pass an index, like pop(0), to remove from somewhere else; see removing an element from a list by index.

Remove the last element with del or slicing

del changes the list in place. A slice like cart[:-1] builds a new list and leaves the original alone, which matters when other variables point to it:

cart = ["Coffee beans", "Mug", "Filters", "Gift card"]
saved = cart                            # a second name for the same list

del cart[-1]                            # in place, returns nothing
print("del cart[-1]:", cart)

shorter = cart[:-1]                     # a NEW list; cart is unchanged
print("cart[:-1]:", shorter, "| cart:", cart)

del cart[-2:]                           # remove the last 2 items
print("del cart[-2:]:", cart, "| saved sees it:", saved)

Output:

del cart[-1]: ['Coffee beans', 'Mug', 'Filters']
cart[:-1]: ['Coffee beans', 'Mug'] | cart: ['Coffee beans', 'Mug', 'Filters']
del cart[-2:]: ['Coffee beans'] | saved sees it: ['Coffee beans']
Command Prompt output of Python del cart[-1], a cart[:-1] slice leaving the original unchanged, and del cart[-2:] removing the last two items
del and del cart[-2:] change the shared list; the slice made a separate copy.

del cart[-n:] is the tidy way to drop the last n items. For removing several items by value or position, see removing multiple elements from a list.

What happens if the list is empty?

pop() and del both raise IndexError on an empty list. Slicing doesn’t. Guard the call when the list might be empty:

orders = []

try:
    orders.pop()
except IndexError as err:
    print("pop():", err)

try:
    del orders[-1]
except IndexError as err:
    print("del:", err)

print("slice:", orders[:-1])            # slicing never raises
last = orders.pop() if orders else None
print("guarded pop:", last)

Output:

pop(): pop from empty list
del: list assignment index out of range
slice: []
guarded pop: None
Command Prompt output of Python IndexError pop from empty list and list assignment index out of range, plus a safe slice and a guarded pop
Both in-place methods raise; the slice and the guarded pop() don’t.

An empty list counts as False, so if orders: is all you need. More on empty lists in creating an empty list in Python.

Which is fastest: pop(), del or slicing?

pop() and del only touch the last slot, while slicing copies the whole list every time. On a 200,000-item list that difference is huge:

import timeit

setup = "data = list(range(200_000))"
tests = {
    "pop()": "data.pop(); data.append(0)",
    "del data[-1]": "del data[-1]; data.append(0)",
    "data = data[:-1]": "data = data[:-1]; data.append(0)",
}
for name, stmt in tests.items():
    t = min(timeit.repeat(stmt, setup=setup, number=1_000, repeat=3))
    print(f"{name:<18} {t * 1000:>8.2f} ms for 1,000 removals")

Output:

pop()                  0.02 ms for 1,000 removals
del data[-1]           0.03 ms for 1,000 removals
data = data[:-1]    1202.24 ms for 1,000 removals
Command Prompt timing of Python pop, del and slicing to remove the last element 1,000 times from a 200,000-item list
pop() and del stay tiny; rebuilding the list with a slice is far slower.

So use slicing only when you really want a separate copy. To empty the list completely, see clearing a list in Python.

Ways to remove the last element compared

I use pop() almost every time, because the removed value is often exactly what I need next. Arrays from the array module and NumPy work a little differently; see removing elements from an array in Python.

MethodChanges the listReturnsEmpty list
lst.pop()YesThe removed itemIndexError
del lst[-1]YesNothingIndexError
lst[:-1]No (new list)The shorter listReturns []
del lst[-n:]YesNothingNo error

You may also find these list guides useful:

Frequently asked questions

How do I remove the last element from a list in Python?

Call my_list.pop(), or del my_list[-1] if you don’t need the removed value.

What’s the difference between pop() and del?

pop() returns the removed item; del doesn’t return anything.

How do I remove the last N items?

Use del my_list[-n:], or my_list[:-n] for a new list.

Why do I get IndexError: pop from empty list?

There’s nothing to remove. Check if my_list: before popping.

Does my_list[:-1] change the original list?

No. It creates a new list without the last item; the original stays the same.

Where is list.pop documented?

See More on Lists in the Python tutorial.