Remove a dictionary from a list of dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove a dictionary from a list of dictionaries in Python #

To remove a dictionary from a list of dictionaries:

  1. Use a for loop to iterate over a copy of the list.
  2. Check if each dictionary is the one to be removed.
  3. Use the list.remove() method to remove the matching dictionary from the list.
main.py
# ✅ remove dictionary from list of dictionaries (for loop) my_list = [ {'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}, ] for item in my_list.copy(): if item.get('id') == 2: my_list.remove(item) break # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(my_list) # ---------------------------------------- my_list = [ {'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}, ] # ✅ remove dictionary from list of dictionaries (list comprehension) new_list = [ item for item in my_list if item.get('id') != 2 ] # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(new_list) # ---------------------------------------- # ✅ remove dictionary from list of dictionaries (with reassignment) my_list[:] = [ item for item in my_list if item.get('id') != 2 ] # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(my_list)

The first example uses a for loop to remove a dictionary from a list of dictionaries.

We used the list.copy() method to get a copy of the list.

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

This is necessary because we aren't allowed to remove elements from a list while iterating over it.

However, we can iterate over a copy of the list and remove elements from the original list.

main.py
my_list = [ {'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}, ] for item in my_list.copy(): if item.get('id') == 2: my_list.remove(item) break # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(my_list)
On each iteration, we check if the current dictionary has an id key with a value of 2 and if the condition is met, we use the list.remove() method to remove it.

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)
If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

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

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

After we have found the dictionary to be removed, we use the break statement to break out of the for loop.

The break statement breaks out of the innermost enclosing for or while loop.

Using the break statement helps us avoid iterating needlessly after we've removed the dictionary from the list.

The most important thing to note when removing items from a list while iterating is to use the list.copy() method to iterate over a copy of the list.

If you try to remove elements from the original list while iterating over it, you might run into difficult to locate bugs.

Alternatively, you can use a list comprehension.

To remove a dictionary from a list of dictionaries:

  1. Use a list comprehension to iterate over the list.
  2. Exclude the matching dictionary from the new list.
  3. The list comprehension will return a new list that doesn't contain the specified dictionary.
main.py
my_list = [ {'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}, ] # ✅ remove dictionary from list of dictionaries (list comprehension) new_list = [ item for item in my_list if item.get('id') != 2 ] # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(new_list) # ---------------------------------------- # ✅ remove dictionary from list of dictionaries (with reassignment) my_list[:] = [ item for item in my_list if item.get('id') != 2 ] # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(my_list)

We used a list comprehension to iterate over the list of dictionaries.

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 dictionary doesn't have an id key with a value of 2 and return the result.

The new list contains the dictionaries that don't have an id key with a value of 2.

Using a list comprehension is more suitable if you have to remove one or more dictionaries from a list. The list comprehension keeps iterating after the first dictionary has been removed.

If you only have to remove a single dictionary from a list, use the for loop approach.

The list comprehension in the first example doesn't mutate the original list, it returns a new list.

If you want to change the contents of the original list, use list slicing.

main.py
my_list = [ {'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}, ] my_list[:] = [ item for item in my_list if item.get('id') != 2 ] # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 3, 'fruit': 'kiwi'}] print(my_list)

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

main.py
my_list = [ {'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}, ] # 👇️ [{'id': 1, 'fruit': 'apple'}, {'id': 2, 'fruit': 'banana'}, {'id': 3, 'fruit': 'kiwi'}] print(my_list[:])
The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach changes the contents of the original 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.