How to Filter Lists of Dictionaries in Python
Filtering lists of dictionaries based on specific criteria is a common task in Python data manipulation.
This guide explores how to filter lists of dictionaries based on:
- The values associated to keys
- The presence of a specific key.
- Unique value in a list.
Filtering by Value
List comprehensions provide a concise way to filter dictionaries based on their values.
Checking for a Single Value
To find all dictionaries where a key matches a single, specific value:
list_of_dictionaries = [
{'id': 1, 'name': 'alice'},
{'id': 2, 'name': 'anna'},
{'id': 3, 'name': 'carl'},
{'id': 4, 'name': 'anna'},
]
filtered_list_of_dicts = [
dictionary for dictionary in list_of_dictionaries
if dictionary['name'] == 'anna'
]
print(filtered_list_of_dicts)
# Output: [{'id': 2, 'name': 'anna'}, {'id': 4, 'name': 'anna'}]
- The list comprehension filters the
list_of_dictionariesto return dictionaries where the value for key'name'is equal to"anna".
Checking for Multiple Values
To filter based on multiple acceptable values, store them in a list and use the in operator:
list_of_dictionaries = [
{'id': 1, 'name': 'alice'},
{'id': 2, 'name': 'anna'},
{'id': 3, 'name': 'carl'},
]
list_of_values = [1, 3]
filtered_list_of_dicts = [
dictionary for dictionary in list_of_dictionaries
if dictionary['id'] in list_of_values
]
print(filtered_list_of_dicts)
# Output: [{'id': 1, 'name': 'alice'}, {'id': 3, 'name': 'carl'}]
- The
inkeyword is used to check if theidis one of the specifiedlist_of_values.
Using a for Loop
The same filtering can be achieved with a for loop, although this is usually more verbose:
list_of_dictionaries = [
{'id': 1, 'name': 'alice'},
{'id': 2, 'name': 'anna'},
{'id': 3, 'name': 'carl'},
]
list_of_values = [1, 3]
filtered_list_of_dicts = []
for dictionary in list_of_dictionaries:
if dictionary['id'] in list_of_values:
filtered_list_of_dicts.append(dictionary)
print(filtered_list_of_dicts)
# Output: [{'id': 1, 'name': 'alice'}, {'id': 3, 'name': 'carl'}]
- The
forloop will add to thefiltered_list_of_dictsall dictionaries whereidkey matches thelist_of_valueslist.
Filtering for Unique Values
To filter a list of dictionaries, keeping only the first dictionary encountered for each unique value of a specific key, use a dictionary comprehension:
list_of_dictionaries = [
{'id': 1, 'name': 'alice'},
{'id': 2, 'name': 'anna'},
{'id': 1, 'name': 'carl'}, # Duplicate id
]
list_of_unique_dictionaries = list(
{
dictionary['id']: dictionary
for dictionary in list_of_dictionaries
}.values()
)
print(list_of_unique_dictionaries)
# Output: [{'id': 1, 'name': 'carl'}, {'id': 2, 'name': 'anna'}]
- The dictionary comprehension
{dictionary['id']: dictionary for dictionary in list_of_dictionaries}creates a dictionary using the'id'as keys. Because dictionary keys must be unique, later dictionaries with the same'id'overwrite earlier ones. This has the effect of keeping only the last dictionary with a given ID. .values()extracts the dictionary values (the dictionaries themselves).list()then transforms the result to a list.
Filtering Based on Key Existence
To filter a list of dictionaries, keeping only those that have a specific key, use the dict.get() method within a list comprehension:
list_of_dictionaries = [
{'id': 1, 'name': 'alice'},
{'id': 2, 'name': 'anna'},
{'id': 3, 'salary': 100}, # No 'name' key
]
filtered_list = [
dictionary for dictionary in list_of_dictionaries
if dictionary.get('name') is not None
]
print(filtered_list)
# Output: [{'id': 1, 'name': 'alice'}, {'id': 2, 'name': 'anna'}]
dictionary.get('name')attempts to retrieve the value associated with the key'name'. If the key is not present, it returnsNone(by default) instead of raising aKeyError.is not Noneensures that only dictionaries with the 'name' key are included.