W3docs

Remove List Items

Learn every way to remove items from a Python list: remove(), pop(), del, clear(), and list comprehensions, with examples and gotchas.

Python lists are mutable sequences, so you can delete elements at any time. This chapter covers every built-in tool for removing list items: remove(), pop(), the del statement, clear(), and list comprehensions. Each approach is suited to a different situation, and knowing when to pick which one prevents common bugs.

Quick reference

Method / statementRemoves byReturns removed item?Error if target missing?
list.remove(value)First matching valueNoValueError
list.pop()Last item (default)YesIndexError on empty list
list.pop(index)Item at indexYesIndexError if out of range
del list[index]Item at indexNoIndexError if out of range
del list[start:stop]Slice of itemsNoNever (empty slice is fine)
list.clear()All itemsNoNever
List comprehensionItems matching a conditionNo (returns new list)Never

The remove() method

list.remove(value) finds the first occurrence of value and deletes it. If the value is not present, Python raises a ValueError.

Remove an item by its value

python— editable, runs on the server

Only the first occurrence is removed

When a value appears more than once, remove() deletes only the first match and leaves the rest:

my_list = [1, 3, 2, 3, 4, 3]
my_list.remove(3)
print(my_list)  # [1, 2, 3, 4, 3]

Guarding against ValueError

Always check that the value exists before calling remove(), or catch the exception:

Check with in before removing

python— editable, runs on the server

Catch the exception

my_list = [1, 2, 4, 5]
try:
    my_list.remove(3)
except ValueError:
    print("3 is not in the list")
# Output: 3 is not in the list

Removing all occurrences with a loop

Use a while loop to keep removing a value until none remain:

Remove every occurrence of an element

python— editable, runs on the server

The pop() method

list.pop(index) removes the item at index and returns it so you can use the value. If you omit the index, it removes and returns the last item. This makes pop() ideal when you need to process the removed value.

Remove the last item

python— editable, runs on the server

Remove an item at a specific index

my_list = [1, 2, 3, 4, 5]
item = my_list.pop(1)   # removes and returns 2
print(item)             # 2
print(my_list)          # [1, 3, 4, 5]

IndexError from pop()

Calling pop() on an empty list, or with an index that is out of range, raises an IndexError:

empty = []
empty.pop()   # IndexError: pop from empty list

my_list = [1, 2, 3]
my_list.pop(10)   # IndexError: pop index out of range

The del statement

del removes an item (or a slice of items) from a list by position, without returning anything.

Delete a single item by index

Remove the item at index 2

python— editable, runs on the server

Delete a slice of items

del accepts the same slice notation as indexing, making it easy to remove a range of elements in one step:

my_list = [1, 2, 3, 4, 5]
del my_list[1:3]   # removes index 1 and 2 (values 2 and 3)
print(my_list)     # [1, 4, 5]

Deleting an empty or out-of-bounds slice never raises an error — it simply has no effect.

Delete the entire list variable

del can also remove the list variable itself, not just its contents:

my_list = [1, 2, 3]
del my_list
# my_list is now undefined; referencing it raises NameError

The clear() method

list.clear() removes every element from the list, leaving an empty list. The list object itself still exists (unlike del my_list).

my_list = [1, 2, 3, 4, 5]
my_list.clear()
print(my_list)  # []

clear() is equivalent to del my_list[:] (deleting the entire slice), but it reads more clearly.

List comprehension for conditional removal

List comprehension builds a new list by including only elements that pass a condition, effectively filtering out the ones you want removed. This is the clearest way to remove all items that match a complex condition.

Remove all occurrences of a value

python— editable, runs on the server

Remove all even numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
odd_only = [x for x in numbers if x % 2 != 0]
print(odd_only)  # [1, 3, 5, 7]

Because a comprehension returns a new list rather than modifying the original, it is safe to use when you need to keep the original unchanged, or when your filter condition involves multiple values at once.

Choosing the right approach

  • Know the value, not the index? Use remove() (first match) or a list comprehension (all matches or complex conditions).
  • Know the index? Use pop(index) if you need the value back, or del list[index] if you do not.
  • Need to empty the whole list? Use clear().
  • Need to remove a range of consecutive items? Use del list[start:stop].

Practice

Practice
Which of the following can be used to remove items from a Python list?
Which of the following can be used to remove items from a Python list?
Was this page helpful?