How to remove items from a Python list?

By Abhinash & Priya Chetty on August 19, 2022

There are several methods to delete or remove items from a Python list.

Remove a single item from the Python list with ‘del’

The delete function, ‘del’ can be used for collections, specifically, lists and dictionaries in Python. To remove a single element or a range of elements specify the list indexes.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
#Determine the index position of c
d = letters.index('c') 
#Delete c from the list
del letters[d]
print(letters)
#Output
['P', 'r', 'o', 'u', 'r', 'e']

The del function simply updates the list, without returning any value. Therefore, use the print( ) for output.

Remove a range of items from a Python list

To delete a range of items from a Python list, the index range has to be mentioned as a list. This index range should start from the position of the first item to be deleted, till the index of the last item to be deleted.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
#Delete a range of items from the list
del letters[1:4]
print(letters)
#Output
['P', 'u', 'r', 'e']

Deleting a list

Lastly, using the del function, an entire list can also be deleted.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
del letters
print(letters)
#Output
NameError: name 'letters' is not defined

The print function throws an error as the list ‘letters’ was deleted and do not exist anymore.

Remove items using the list.remove( ) function

To remove a specific element from an existing list, the list.remove(item) function can be used. This is different from the ‘del’ function, which removes an element present at a specific index. Whereas, the ‘remove’ function, removes the first matching value in the list.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
letters.remove('r')  
print(letters)
#Output
['P', 'o', 'c', 'u', 'r', 'e']

Removing items by replacing the list with an empty list

Items of a Python list can also be removed, by replacing an index range with an empty list. In this case, the Python interpreter will update the items within the range from the new list.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
#Replacing items with an empty list 
letters[1:4] = [ ]
print(letters)
#Output
['P', 'u', 'r', 'e']

Remove the list items by slicing

Slicing an entire list will remove all of the list items and an empty list will be generated.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
letters[:] = [ ]
print(letters)
#Output
[]

Here, the range defined has no indexes, suggesting that the function has to be implemented upon the entire list. By assigning the entire range of items to an empty list, Python clears the whole list.

Pop the list item

Pop refers to a combination of removing and returning items from a list. Using the list.pop( ) function, it is possible to remove a particular item from a list. In case the index is not provided, the Python interpreter will remove and return the last element. Popping is similar to the ‘del’ function but pop returns the removed element, whereas the ‘del’ function doesn’t.

letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
print(letters.pop(5))
print(letters)
#Output
r
['P', 'r', 'o', 'c', 'u', 'e']

Discuss