Remove empty dictionaries from list of dicts in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove empty dictionaries from list of dicts in Python #

Use a list comprehension to remove the empty dictionaries from a list of dictionaries, e.g. new_list = [item for item in list_of_dicts if item]. The list comprehension will return a new list that doesn't contain any empty dictionaries.

main.py
# ✅ Remove empty dictionaries from a list (using list comprehension) list_of_dicts = [{'id': 1}, {}, {'id': 2}, {}, {'id': 3}] new_list = [item for item in list_of_dicts if item] print(new_list) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}] # --------------------------------------------- # ✅ Remove empty dictionaries from a list (using filter()) new_list = list(filter(None, list_of_dicts)) print(new_list) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}] # --------------------------------------------- # ✅ Remove empty dictionaries from a list (using for loop) for item in list_of_dicts.copy(): if item == {}: list_of_dicts.remove(item) print(list_of_dicts) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}]

The first example uses a list comprehension to remove the empty dictionaries from a list of dictionaries.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
list_of_dicts = [{'id': 1}, {}, {'id': 2}, {}, {'id': 3}] new_list = [item for item in list_of_dicts if item] print(new_list) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}]

On each iteration, we check if the current item is truthy and return the result.

Empty dictionaries are falsy values, so they get filtered out.

The list comprehension creates a new list, it doesn't mutate the original list.

If you want to remove the empty dictionaries from the original list, use list slicing.

main.py
list_of_dicts = [{'id': 1}, {}, {'id': 2}, {}, {'id': 3}] list_of_dicts[:] = [item for item in list_of_dicts if item] print(list_of_dicts) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}]

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

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.

Alternatively, you can use the filter() function.

Remove empty dictionaries from list of dicts using filter() #

To remove the empty dictionaries from a list of dictionaries:

  1. Use the filter() function to filter out the empty dictionaries.
  2. Use the list() class to convert the filter object to a list.
  3. The new list won't contain any empty lists.
main.py
list_of_dicts = [{'id': 1}, {}, {'id': 2}, {}, {'id': 3}] new_list = list(filter(None, list_of_dicts)) print(new_list) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}]

We used the filter() function to remove the empty dictionaries from a list of dictionaries.

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.

An empty dictionary is a falsy value, so all empty dictionaries get removed.

The last step is to use the list() class to convert the filter object to a list.

Alternatively, you can use a for loop.

Remove empty dictionaries from list of dicts using for loop #

To remove the empty dictionaries from a list of dictionaries:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if the current item is an empty dictionary.
  3. Use the list.remove() method to remove the empty dictionaries.
main.py
list_of_dicts = [{'id': 1}, {}, {'id': 2}, {}, {'id': 3}] for item in list_of_dicts.copy(): if item == {}: list_of_dicts.remove(item) print(list_of_dicts) # 👉️ [{'id': 1}, {'id': 2}, {'id': 3}]
On each iteration, we check if the current item is an empty dictionary and use the list.remove() method to remove the matching elements.

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.

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

If you try to iterate over the original list and remove items from it, you might run into difficult to locate bugs.

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.