Reputation: 546
How do I get the number of dictionaries where value satisfies a given condition?
list_of_dict = [
{'seq_no': 10, 'file_path': 'file/path', 'rel_item': None},
{'seq_no': 22, 'file_path': 'file/path', 'rel_item': 0},
{'seq_no': 32, 'file_path': 'file/path', 'rel_item': 0},
{'seq_no': 90, 'file_path': 'file/path', 'rel_item': 0},
{'seq_no': 10, 'file_path': 'file/path', 'rel_item': None},
]
I would like to count the number of dictionaries where key rel_item=None
.
Upvotes: 0
Views: 3915
Reputation: 5666
You can filter
the list_of_dict
based on your criteria
>>> len(list(filter(lambda x: x['rel_item'] is None, list_of_dict)))
>>> 2
Upvotes: 1
Reputation: 323286
You can using map
then
sum((map(lambda x : x['rel_item'] is None,list_of_dict)))
Out[721]: 2
Upvotes: 0
Reputation: 164693
Here is one way.
c = sum(i['rel_item'] is None for i in list_of_dict)
# 2
Upvotes: 4
Reputation: 9018
This will do the job:
len([x for x in list_of_dict if x["rel_item"] == None])
Upvotes: 1