Filter a list of Python dictionaries by conditions

In this post, we are going to understand in Filter a list of Python dictionaries by conditions on keys or values. If we have a dictionary having huge data, it is better will filter the dictionary based on conditions.

1 . Filter a list of dictionaries by value


In this code example, we are filtering the dictionaries by value based on conditions. We have stud_dict, and we are filtering dictionaries key which has age ==20, We can change condition based on our requirment.

So the resulting output is only the dictionaries that pass given conditions on values. Here is the code example to understand this.

Code Example

#Program to  filter a list of Python Dictionary


stu_dict = [{'name':'alice','sub':'math','age':25},
{'name':'jack','sub':'math','age':22},
{'name':'Ron','Sub':'eng','age':20},
{'name':'Sam','Sub':'Phy','age':23}]
student = [stu for stu in stu_dict if stu['age']==20]

print('filtered dictionary based on key =\n',student)


Output

filtered dictionary based on key =
 [{'name': 'Ron', 'Sub': 'eng', 'age': 20}]

2.Filter list of dictionaries by key


In this code example, we are filtering the dictionaries by key based on conditions. We have stud_dict, and we are filtering dictionaries which have key name ‘sub’, We can change condition based on our requirment. So the resulting output is only the dictionaries that pass given condition on keys. Here is the code example to understand this.

Code Example

#Program to  filter a list of Python Dictionary

stu_dict = [{'name':'alice','sub':'math','age':25},
{'name':'jack','sub':'math','age':22},
{'name':'Ron','age':20},
{'name':'Sam','age':23}]

result =  [stu for stu in stu_dict if 'sub' in stu]


print('filtered dictionary based on key =\n',result)

Output

filtered dictionary based on value =
 [{'name': 'alice', 'sub': 'math', 'age': 25}, {'name': 'jack', 'sub': 'math', 'age': 22}]

3. Filter list of dictionaries based on multiple keys


In this code example, we have a list of keys (list_keys) to filter the dictionaries based on condition by multiples keys by using a list of comprehension. So we are filtering the list of dictionaries stu_dict.

Code Example

#Program to  filter a list of Python Dictionary

stu_dict = [{'name':'alice','sub':'math','age':25},
{'name':'jack','sub':'math','age':22},{'name':'Ron','age':20}, {'Standard':'10th','teacher':'Ronak'}]

#keys to filter
list_keys = ['sub','age','name']


filter_dict = [{key:val for key, val in ele.items() if key in list_keys} for ele in stu_dict]

print('filtered dictionary based on mutiple keys =\n',filter_dict)

Output

filtered dictionary based on multiple keys =
 [{'name': 'alice', 'sub': 'math', 'age': 25}, {'name': 'jack', 'sub': 'math', 'age': 22}, {'name': 'Ron', 'age': 20}, {}]

4.filter list of dictionaries based on multiple keys using map()


In this code example, we have a list of keys (list_keys) to filter the dictionaries based on condition by multiples keys by using a list of comprehension. So we are filtering the list of dictionaries stu_dict.

Code Example

#Program to  filter a list of Python Dictionary

stu_dict = [{'name':'alice','sub':'math','age':25},
{'name':'jack','sub':'math','age':22},{'name':'Ron','age':20}, {'class':'10th','teacher':'Ronak'}]

#keys to filter
list_keys = {'sub','age','name'}
filter_Dict = list(map(lambda x: {k:v for k, v in x.items() if k in list_keys}, stu_dict))


print('filtered dictionary based on mutiple keys =\n',filter_Dict)

Output

filtered dictionary based on multiple keys =
 [{'name': 'alice', 'sub': 'math', 'age': 25}, {'name': 'jack', 'sub': 'math', 'age': 22}, {'name': 'Ron', 'age': 20}, {}]

5. Filter() to filter a list of dictionaries by multiple values


In this code example, we are filtering the list of dictionaries by a list of values using the filter() method. We want to filter a dictionary that has key-value ==20 and the key name has value == Alice. If we need we can change the condition according to our requirements.As we can see in the output shows a list of dictionaries that satisfy the given condition

#Program to  filter a list of Python Dictionary

stu_dict = [{'name':'alice','sub':'math','age':20},
{'name':'jack','sub':'math','age':23},{'name':'Ron','age':25}]

#keys to filter
list_keys = [20,'alice']
filter_Dict = list(filter(lambda d: d['age'] in list_keys, stu_dict))


print('filtered dictionary based on mutiple keys =\n',filter_Dict)

Output

filtered dictionary based on mutiple keys =
 [{'name': 'alice', 'sub': 'math', 'age': 20}]

Conclusion

In this post, we have understood how to Filter a list of Python dictionaries by conditions on mutiple keys or values with code examples by using all these code examples we can easily filter a list of dictionaries.

Happy Learning!!