Remove List Items

Removing items from a list is a common operation in Python. Because lists are mutable, Python provides multiple ways to remove elements by value, by index, or completely.
This topic covers all correct ways to remove list items, including edge cases and best practices.

Remove Item Using remove()

The remove() method removes the first occurrence of a specified value.
python
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)
python
numbers = [1, 2, 3, 2]
numbers.remove(2)
print(numbers)

Important Note

If the value does not exist, Python raises an error.
python
# fruits.remove("mango")  # ValueError

Remove Item Using pop()

The pop() method removes an item by index and returns it.

Remove Last Item

python
fruits = ["apple", "banana", "cherry"]
item = fruits.pop()
print(item)
print(fruits)

Remove Item at Specific Index

python
fruits = ["apple", "banana", "cherry"]
fruits.pop(1)
print(fruits)

Remove Item Using del

The del keyword removes an item by index or deletes the entire list.

Delete Item at Index

python
numbers = [10, 20, 30]
del numbers[1]
print(numbers)

Delete a Slice

python
numbers = [1, 2, 3, 4, 5]
del numbers[1:4]
print(numbers)

Delete Entire List

python
numbers = [1, 2, 3]
del numbers

Remove All Items Using clear()

The clear() method removes all elements from the list.
python
numbers = [1, 2, 3]
numbers.clear()
print(numbers)

Remove Items Using a Loop

Remove All Occurrences of a Value

python
numbers = [1, 2, 3, 2, 2]

while 2 in numbers:
    numbers.remove(2)

print(numbers)

Remove Using List Comprehension

python
numbers = [1, 2, 3, 4, 5]
numbers = [x for x in numbers if x % 2 != 0]
print(numbers)

Remove Items Conditionally

python
numbers = [10, 20, 30, 40]

numbers = [x for x in numbers if x != 20]
print(numbers)

Common Mistakes

Modifying List While Iterating

python
nums = [1, 2, 3, 4]

for n in nums:
    if n % 2 == 0:
        nums.remove(n)

print(nums)
This produces unexpected results.

Safe Way to Remove While Iterating

python
nums = [1, 2, 3, 4]
nums = [n for n in nums if n % 2 != 0]
print(nums)

Difference Between remove(), pop(), and del

MethodRemoves byReturns valueRaises error
remove()ValueNoIf value not found
pop()IndexYesIf index invalid
delIndex / SliceNoIf index invalid
clear()All itemsNoNo

Summary

  • remove() deletes by value
  • pop() deletes by index and returns item
  • del deletes by index, slice, or entire list
  • clear() empties the list
  • Use list comprehension for conditional removal
  • Avoid modifying list while iterating