Filter List of Dictionaries Based on Key Values in Python
Last Updated :
24 Dec, 2024
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.
Python
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:
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().
Python
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.
Python
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.
Python
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'}]
Similar Reads
Python - Keys associated with value list in dictionary Sometimes, while working with Python dictionaries, we can have a problem finding the key of a particular value in the value list. This problem is quite common and can have applications in many domains. Let us discuss certain ways in which we can Get Keys associated with Values in the Dictionary in P
4 min read
Flatten given list of dictionaries - Python We are given a list of dictionaries, and the task is to combine all the key-value pairs into a single dictionary. For example, if we have: d = [{'a': 1}, {'b': 2}, {'c': 3}] then the output will be {'a': 1, 'b': 2, 'c': 3}Using the update() methodIn this method we process each dictionary in the list
3 min read
Iterate through list of dictionaries in Python In this article, we will learn how to iterate through a list of dictionaries. List of dictionaries in use: [{'Python': 'Machine Learning', 'R': 'Machine learning'}, {'Python': 'Web development', 'Java Script': 'Web Development', 'HTML': 'Web Development'}, {'C++': 'Game Development', 'Python': 'Game
3 min read
How to use a List as a key of a Dictionary in Python 3? In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained
3 min read
Get Key from Value in Dictionary - Python The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value. Using next() with a Generator ExpressionThis is the most efficient when we only need the first matching key. This meth
5 min read