Python – Remove List Items

In Python, you can remove items from a list using several built-in methods. These methods allow you to remove by value, index, or even clear the entire list.

Example List for Reference

fruits = ["apple", "banana", "cherry", "banana", "orange"]

Index Reference:

Index:     0        1         2         3         4
Items:   "apple"  "banana"  "cherry"  "banana"  "orange"

1. remove() – Remove by Value

Removes the first matching value in the list.

Example:

fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits)

Output:

['apple', 'cherry', 'banana']

Only the first "banana" is removed.

⚠️ Raises ValueError if the item is not found.

2. pop() – Remove by Index

Removes and returns the item at a specific index. Default is the last item.

Example 1: Pop last item

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

Output:

cherry
['apple', 'banana']

Example 2: Pop specific index

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

Output:

['banana', 'cherry']

⚠️ Raises IndexError if the index is out of range.

3. del – Delete by Index or Entire List

The del statement can be used to delete an item, a slice, or the entire list.

Example 1: Delete one item

fruits = ["apple", "banana", "cherry"]
del fruits[1]
print(fruits)

Output:

['apple', 'cherry']

Example 2: Delete a slice

fruits = ["apple", "banana", "cherry", "orange"]
del fruits[1:3]
print(fruits)

Output:

['apple', 'orange']

Example 3: Delete entire list

fruits = ["apple", "banana"]
del fruits

⚠️ After del fruits, the list is removed from memory.

4. clear() – Empty the List

Removes all items from the list, but keeps the list itself.

fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)

Output:

[]

5. Loop-Based Removal (Use with Caution)

When removing multiple items in a loop, be careful, as it can cause indexing issues.

Example: Remove all "banana"

fruits = ["apple", "banana", "cherry", "banana"]
fruits = [fruit for fruit in fruits if fruit != "banana"]
print(fruits)

Output:

['apple', 'cherry']

This is a safe and clean way to remove multiple items.

⚠️ Summary Table of Remove Methods

MethodDescriptionModifies ListReturns Value
remove(x)Removes first occurrence of value x
pop(i)Removes and returns item at index i
del list[i]Deletes item or slice at index i
clear()Empties the list