In this Python tutorial, you will learn how to remove a specific item from a list, remove an item at a given index, remove and return an item using pop(), and safely handle missing values and invalid indexes.
Remove an Item from a Python List
Python provides several ways to remove items from a list. The correct method depends on whether you know the item’s value, its index, or whether you also need the removed value.
| Requirement | Python statement | Result |
|---|---|---|
| Remove the first matching value | list.remove(value) | Modifies the list and returns None |
| Remove an item at a known index | del list[index] | Modifies the list without returning the item |
| Remove and return an item | list.pop(index) | Returns the removed item |
| Remove several items by index range | del list[start:stop] | Deletes the selected slice |
| Remove all matching values | List comprehension | Creates a filtered list |
list.remove(object) can be used to remove a specific object from the list. remove() removes only the first occurrence of the object in the list. Subsequent occurrences of the object remain in the list.
del list[index] can be used to remove an object from specific index of the list.
Python List Item Removal Syntax
Syntax to Remove a Value with remove()
list_name.remove(value)
The method searches from the beginning of the list and removes the first element equal to value. It raises ValueError when no matching element exists.
Syntax to Delete an Item by Index with del
del list_name[index]
Python list indexes start at 0. A negative index counts from the end, so -1 identifies the last item.
Syntax to Remove and Return an Item with pop()
removed_item = list_name.pop(index)
The index argument is optional. Calling pop() without an index removes and returns the last item.
Python Examples for Removing List Items
1. Remove a Specific Element from a Python List
To remove a specific element from the list, call remove() on the list with the object passed as argument to remove().
main.py
#initialize list
aList = [True, 28, 'Tiger', 'Lion']
#remove specific item
aList.remove('Tiger')
#print list
print(aList)
Output
[True, 28, 'Lion']
The specified object is removed from the list.
Reference tutorials for the above program
2. Remove an Element That Is Not Present in the List
An attempt to remove a specific object from Python List, where the specified object is not present in the list, results in ValueError.
main.py
#initialize list
aList = [True, 28, 'Tiger', 'Lion']
#remove specific item
aList.remove('Rhino')
#print list
print(aList)
Output
Traceback (most recent call last):
File "main.py", line 5, in <module>
aList.remove('Rhino')
ValueError: list.remove(x): x not in list
ValueError occurs with the message list.remove(x): x not in list.
When a missing value is expected, check membership before calling remove().
animals = ['Tiger', 'Lion']
item = 'Rhino'
if item in animals:
animals.remove(item)
print(animals)
Output
['Tiger', 'Lion']
3. Remove the First of Multiple Matching Elements
In the following example, we initialize a list with an object 'Tiger' having multiple occurrences. When we apply remove('Tiger') on the list, only the first occurrence is removed.
main.py
#initialize list
aList = [True, 28, 'Tiger', 'Lion', 'Tiger']
#remove object
aList.remove('Tiger')
#print list
print(aList)
Output
[True, 28, 'Lion', 'Tiger']
The second occurrence remains because remove() stops after deleting the first match.
4. Remove an Element from a List at a Specific Index
To delete an object at a specific position or index from a List, use del.
The syntax is del list[index].
In the following Python program, we initialize a Python list and delete an item at index 2.
main.py
#initialize list
aList = [True, 28, 'Tiger', 'Lion']
#remove item from specific position
del aList[2]
#print list
print(aList)
Output
[True, 28, 'Lion']
After the deletion, the items that followed index 2 shift one position to the left.
5. Remove the Last Item from a Python List
You can delete the last item with del list_name[-1]. Use pop() instead when you also need the removed value.
animals = ['Tiger', 'Lion', 'Leopard']
del animals[-1]
print(animals)
Output
['Tiger', 'Lion']
6. Remove and Return an Item with pop()
The pop() method removes the item at the specified index and returns it. This is useful when the program must continue using the removed value.
animals = ['Tiger', 'Lion', 'Leopard']
removed_animal = animals.pop(1)
print(removed_animal)
print(animals)
Output
Lion
['Tiger', 'Leopard']
Calling animals.pop() without an index would remove and return 'Leopard', the final item.
7. Remove Multiple Items from a List by Index Range
Use del with a slice to remove consecutive items. The start index is included, while the stop index is excluded.
numbers = [10, 20, 30, 40, 50, 60]
del numbers[1:4]
print(numbers)
Output
[10, 50, 60]
The statement removes the items at indexes 1, 2, and 3.
8. Remove All Occurrences of a Value from a Python List
Because remove() deletes only one occurrence, use a list comprehension when every matching value must be excluded.
animals = ['Tiger', 'Lion', 'Tiger', 'Leopard', 'Tiger']
animals = [animal for animal in animals if animal != 'Tiger']
print(animals)
Output
['Lion', 'Leopard']
This expression creates a new list containing only the items that are not equal to 'Tiger'.
Errors When Removing Python List Items by Index
Using del or pop() with an index outside the list’s valid range raises IndexError.
animals = ['Tiger', 'Lion']
del animals[5]
Output
IndexError: list assignment index out of range
Check that the index is within the valid range before deleting an item.
animals = ['Tiger', 'Lion']
index = 1
if -len(animals) <= index < len(animals):
del animals[index]
print(animals)
Output
['Tiger']
Difference Between remove(), del, pop(), and clear()
| Operation | Chooses item by | Returns removed item | Possible error |
|---|---|---|---|
remove(value) | Value | No | ValueError if the value is absent |
del list[index] | Index or slice | No | IndexError for an invalid single index |
pop(index) | Index | Yes | IndexError for an invalid index or an empty list |
clear() | Entire list | No | No error when the list is already empty |
Use remove() when you know the value, del when you know the index or slice, and pop() when you need the deleted item. Use clear() only when every item should be removed.
Frequently Asked Questions about Removing Python List Items
Does list.remove() delete every matching item?
No. list.remove(value) deletes only the first matching item. Use a list comprehension when all occurrences must be removed.
How do I remove an item from a Python list without an error?
Before using remove(), test whether the value is in the list. Before using del or pop(), verify that the index is within the list’s valid range.
How do I remove the last item from a Python list?
Use del list_name[-1] when the value is not needed, or use list_name.pop() when the removed value must be returned.
Does remove() return the deleted list item?
No. remove() modifies the list and returns None. Use pop() to remove and return an item.
Python List Item Removal Summary
Use list.remove(value) to delete the first item equal to a specified value. Use del list[index] to delete an item at a known position, and use list.pop(index) when you also need the removed value. For repeated values, remember that remove() affects only the first occurrence.
In this Python Tutorial, we learned to remove a particular object from the list or delete an item from given index of the list.
TutorialKart.com