Filter List of Dictionaries Based on Key Values in Python
Filtering a list of dictionaries based on specific key values is a common task in Python. This becomes useful when working with large datasets or when we need to select certain entries that meet specific conditions. We can do this using list comprehensions, the filter() function, and simple loops.
Using List Comprehension
List comprehension is one of the most efficient ways to filter a list. It allows us to create a new list by iterating over an existing list and applying a condition. We loop over each dictionary d in the list people. For each dictionary, we check if the value of the key 'role' is in the f list. If the condition is true, we add that dictionary to the new list.
p = [{'name': 'geek1', 'role': 'Engineer'},
{'name': 'geek2', 'role': 'Manager'},
{'name': 'geek3', 'role': 'Director'},
{'name': 'geek4', 'role': 'CEO'}]
f = ['Engineer', 'Designer']
# Use list comprehension to filter the list based on roles
res = [d for d in p if d['role'] in f]
print(res)
Output
[{'name': 'geek1', 'role': 'Engineer'}]
Other methods of Filtering a list of dictionaries based on key values are:
Table of Content
Using filter() Function with lambda
The filter() function is another way to filter a list. The filter() function applies a condition to each dictionary in people. We use a lambda function to check if the value of the 'role' key is in the f list. The filter() function returns an iterator, so we need to convert it to a list using list().
p = [{'name': 'geek1', 'role': 'Engineer'},
{'name': 'geek2', 'role': 'Manager'},
{'name': 'geek3', 'role': 'Director'},
{'name': 'geek4', 'role': 'CEO'}]
f = ['Engineer', 'Designer']
# Use filter() with lambda to filter the list based on roles
res = list(filter(lambda d: d['role'] in f, p))
print(res)
Output
[{'name': 'geek1', 'role': 'Engineer'}]
Using filter() with regular function
Instead of using a lambda function, we can define a regular function to check the condition. This can be useful if we prefer a more readable and reusable solution. We define a separate function fun() that checks whether the 'role' of each dictionary is in the f list. The filter() function applies fun() to each dictionary in people. As with the previous method, we convert the result of filter() to a list.
p = [{'name': 'geek1', 'role': 'Engineer'},
{'name': 'geek2', 'role': 'Manager'},
{'name': 'geek3', 'role': 'Director'},
{'name': 'geek4', 'role': 'CEO'}]
f = ['Engineer', 'Designer']
# Define a function to filter the dictionaries
def fun(d):
return d['role'] in f
# Use filter() with the filter_func function
res = list(filter(fun, p))
print(res)
Output
[{'name': 'geek1', 'role': 'Engineer'}]
Using for Loop
If we're new to Python, using a for loop might feel more natural. This method is the most explicit, where you manually iterate through each dictionary and apply the condition. We loop over each dictionary in people. For each dictionary, we check if the 'role' key is in the f list. If the condition is true, we append the dictionary to the result list result.
p = [{'name': 'geek1', 'role': 'Engineer'},
{'name': 'geek2', 'role': 'Manager'},
{'name': 'geek3', 'role': 'Director'},
{'name': 'geek4', 'role': 'CEO'}]
f = ['Engineer', 'Designer']
# Use a for loop to filter the list
res = []
for d in p:
if d['role'] in f:
res.append(d)
print(res)
Output
[{'name': 'geek1', 'role': 'Engineer'}]