7

I have a list of dictionaries (abbreviated).

my_list = [{ 'id':1, 'val':123 }, {'id':2, 'val':456 }, {'id':2, 'val':789 }]

How can I count the occurrences of dictionaries with a specified value for a particular key (in this case 'id')? Is there a way to leverage count (my_list.count('id' = 1) ?!?)

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Jamie
  • 7,075
  • 12
  • 56
  • 86

2 Answers2

9

How about

sum(1 for d in my_list if d.get('id') == the_value_you_are_interested_in)

>>> my_list = [{ 'id':1, 'val':123 }, {'id':2, 'val':456 }, {'id':2, 'val':789 }]
>>> sum(1 for d in my_list if d.get('id') == 1)
1
>>> sum(1 for d in my_list if d.get('id') == 2)
2
>>> sum(1 for d in my_list if d.get('id') == 20)
0

Note the use of the generator rather than a list of 1s. This is a pretty established technique and probably appears on several Stack Overflow questions.

I don't see any way to leverage list.count(x) since this method counts the number of occurrences of x, which in your case would be complete dictionaries. Python does have a filter method, but comprehensions are much preferred.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • I do not understand your message in last line in answer - python learner. – Grijesh Chauhan Sep 22 '13 at 05:12
  • 2
    If I write `sum([1 for x in a])` and `a` is a huge list, then the list comprehension theoretically generates a huge list of 1s, THEN sums them all up. But if I write `sum(1 for x in a)` then the 1s are summed as `a` is being iterated on, without the large structure being realized. – Ray Toal Sep 22 '13 at 05:16
  • Got It now! very useful information for me. Thanks you very much. – Grijesh Chauhan Sep 22 '13 at 05:19
  • One more thing you says "compression are preferred over `filter()`" but I read in Apress-Beginning that filter, map, reduction are much faster than list-compressions. – Grijesh Chauhan Sep 22 '13 at 05:24
  • 1
    @GrijeshChauhan Much faster? I don't think so. Map+filter might sometimes be faster than a comprehension, but not by much. If you have a large list use a generator instead of a comprehension in case a lazy map is the thing that is "much faster". See for example http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map – Ray Toal Sep 22 '13 at 05:49
  • Ok Got it, filter, map, reduction are slightly faster but list list-comprehensions are better readable. Thanks. – Grijesh Chauhan Sep 22 '13 at 05:57
6

I like @Ray's answer. Another cool trick is to use collections.Counter.

from collections import Counter

c = Counter( item for dct in my_list for item in dct.items() )
c
=> Counter({('id', 2): 2, ('val', 123): 1, ('id', 1): 1, ('val', 456): 1, ('val', 789): 1})

c[('id', 2)]
=> 2
c[('id', 1)]
=> 1
c[('id', 20)]
=> 0

This solution is particularly good if you need to count multiple keys/values.

If you only care about a particular key, you can do:

k = 'id'
id_counter = Counter( dct.get(k) for dct in my_list )
id_counter
=> Counter({2: 2, 1: 1})
id_counter[2]
=> 2
shx2
  • 61,779
  • 13
  • 130
  • 153