Remove an item from Python List

You can remove or delete an object from Python List at specified position; or delete a specific object from the 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.

Remove specific object from Python List

To remove a specific object from the list, call remove() on the list with the object passed as argument to remove().

example.py – Python Program

#initialize list
aList = [True, 28, 'Tiger', 'Lion']

#remove specific item
aList.remove('Tiger')

#print list
print(aList)
Try Online

Output

[True, 28, 'Lion']

The specified object is removed from the list.

Try Removing object that is not present from Python List

An attempt to remove a specific object from Python List, where the specified object is not present in the list, results in ValueError.

example.py – Python Program

#initialize list
aList = [True, 28, 'Tiger', 'Lion']

#remove specific item
aList.remove('Rhino')

#print list
print(aList)
Try Online

Output

Traceback (most recent call last):
  File "example1.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.

Try Removing object that has multiple occurrences in the Python List

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.

example.py – Python Program

#initialize list
aList = [True, 28, 'Tiger', 'Lion', 'Tiger']

#remove object
aList.remove('Tiger')

#print list
print(aList)
Try Online

Output

[True, 28, 'Lion', 'Tiger']

Remove Object from Python List at a specified position

To delete an object at a specific position from Python 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.

example.py – Python Program

#initialize list
aList = [True, 28, 'Tiger', 'Lion']

#remove item from specific position
del aList[2]

#print list
print(aList)
Try Online

Output

[True, 28, 'Lion']

Conclusion

In this Python Tutorial, we learned to remove a particular object from the list or delete an item from given index of the list.