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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -