Remove an object from a list of objects in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove an object from a list of objects in Python #

To remove an object from a list of objects:

  1. Use the pop() method to remove an object from a list by index.
  2. Use the remove() method to remove an object from a list by value.
main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] # ✅ Remove object from list by index my_list.pop(0) print(my_list) # 👉️ [{'b': 2}, {'c': 3}, {'d': 4}] # -------------------------------------------- # ✅ Remove object from list by value my_list.remove({'d': 4}) print(my_list) # 👉️ [{'b': 2}, {'c': 3}] # -------------------------------------------- # ✅ Remove object from list using for loop my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] for item in my_list.copy(): if item.get('b') == 2: my_list.remove(item) break print(my_list) # 👉️ [{'c': 3}]

The first example uses the list.pop() method to remove an object from a list by index.

main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] my_list.pop(0) print(my_list) # 👉️ [{'b': 2}, {'c': 3}, {'d': 4}]

The list.pop method removes the item at the given position in the list and returns it.

If no index is specified, the pop() method removes and returns the last item in the list.

You might also use the del operator to remove an object from a list by index.

main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] del my_list[0] print(my_list) # 👉️ [{'b': 2}, {'c': 3}, {'d': 4}]

The pop() method and the del operator raise an IndexError if the specified index is out of range.

You can use a try/except statement if you need to handle that scenario.

main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] try: my_list.pop(100) except IndexError: # 👇️ this runs print('Specified index out of range')

The given index is out of range, so the except block runs.

You can use the list.remove() method if you need to remove an object from a list by value.
main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] my_list.remove({'d': 4}) print(my_list) # 👉️ [{'a': 1}, {'b': 2}, {'c': 3}]

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The method raises a ValueError if there is no such item.

The remove() method mutates the original list and returns None.

If you need to handle a scenario where the item is not present in the list, use a try/except statement.

main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] try: my_list.remove({'e': 100}) print(my_list) except ValueError: # 👇️ this runs print('Item not present in the list')

Alternatively, you can use a for loop to remove an object from a list.

main.py
my_list = [{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}] for item in my_list.copy(): if item.get('b') == 2: my_list.remove(item) break print(my_list) # 👉️ [{'a': 1}, {'c': 3}, {'d': 4}]

We used a for loop to iterate over a copy of the list.

The list.copy method returns a shallow copy of the object on which the method was called.

Using the copy() method is necessary because it's not allowed to remove items from a list while iterating over it.

However, it is allowed to iterate over a copy of the list and remove items from the original list.

We used the break keyword to exit the loop after we remove the element.

If you need to remove multiple elements from the list, remove the break keyword.

Alternatively, you can use a list comprehension.

main.py
my_list = ['a', 'b', 'a', 'c', 'b', 'a'] new_list = [item for item in my_list if item != 'a'] print(new_list) # 👉️ ['b', 'c', 'b']
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current list item is not equal to a and return the result.

Using a list comprehension is quite convenient when we need to filter out multiple values from the list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.