get count of values associated with key in dict python -
list of dict .
[{'id': 19, 'success': true, 'title': u'apple'}, {'id': 19, 'success': false, 'title': u'some other '}, {'id': 19, 'success': false, 'title': u'dont know'}] i want count of how many dict have success true.
i have tried,
len(filter(lambda x: x, [i['success'] in s])) how can make more elegant using pythonic way ?
you use sum() add boolean values; true 1 in numeric context, false 0:
sum(d['success'] d in s) this works because python bool type subclass of int, historic reasons.
if wanted make explicit, use conditional expression, readability not improved in opinion:
sum(1 if d['success'] else 0 d in s)
Comments
Post a Comment