Python provides several ways to remove items from a list. The method to use depends on whether we want to remove an item by its value, index, a range of indices, or remove all elements. For example, remove() can be used to remove the first occurrence of a specified value from a list.
a = [10, 20, 30, 20, 40, 50]
a.remove(20)
print(a)
Output
[10, 30, 20, 40, 50]
Using remove()
remove() method removes the first occurrence of a specified value from the list. If the value appears multiple times, only the first occurrence is removed.
a = [10, 20, 30, 20, 40, 50]
a.remove(20)
print(a)
Output
[10, 30, 20, 40, 50]
Explanation:
- remove(20) searches for the first occurrence of 20.
- The first 20 is removed from the list.
- The second 20 remains unchanged.
- The list is modified in place.
Note: If the specified value is not present in the list, remove() raises a ValueError.
Using pop()
pop() method removes an item using its index and returns the removed element. It is useful when we need both the updated list and the removed value.
a = [10, 20, 30, 40, 50]
val = a.pop(1)
print(a)
print("Removed Item:", val)
Output
[10, 30, 40, 50] Removed Item: 20
Explanation:
- pop(1) removes the element at index 1.
- The removed value is stored in val.
- The original list is modified.
- pop() returns the removed element.
Using pop() Without an Index
If no index is specified, pop() removes and returns the last element of the list.
a = [10, 20, 30, 40, 50]
val = a.pop()
print(a)
print("Removed Item:", val)
Output
[10, 20, 30, 40] Removed Item: 50
Explanation:
- pop(1) removes the element at index 1.
- The element at index 1 is 20.
- The removed element is stored in val.
- pop() modifies the original list.
- pop() returns the removed element.
Using del()
The del statement removes an element by its index or removes multiple elements using a slice.
Removing a single element by index
a = [10, 20, 30, 40, 50]
# Removes element at index 2 (which is 30).
del a[2]
print(a)
Output
[10, 20, 40, 50]
Explanation:
- a[2] refers to the element at index 2.
- del removes that element from the list.
- The list is modified in place.
Removing multiple elements by index range
A slice can be used with del to remove multiple elements from a list.
a = [10, 20, 30, 40, 50, 60, 70]
del a[1:4]
print(a)
Output
[10, 50, 60, 70]
Explanation:
- a[1:4] selects elements at indices 1, 2, and 3.
- del removes these elements from the list.
- The element at index 4 is not removed because the ending index is exclusive.
Using clear()
clear() method removes all elements from a list while keeping the list object.
a = [10, 20, 30, 40]
a.clear()
print(a)
Output
[]
Explanation:
- clear() removes every element from the list.
- The list remains available after the operation.
- The resulting list is empty.
Which method to use?
| Method | Removes By | Returns Removed Element | Modifies List In-Place |
|---|---|---|---|
remove() | Remove item by Value | No | Yes |
pop() | Remove item by Index (or last item) | Yes | Yes |
del | Remove by Index or range | No | Yes |
clear() | Remove all elements | No | Yes |