I want to count the number of times specific values appear in a list of dictionaries. However, I know that some of these dictionaries will not have the key. I do not know which though, because these are results from an API call.
As an example, this code works for key1
, because all of the dictionaries have the key.
from collections import Counter
list_of_dicts = [
{'key1': 'testing', 'key2': 'testing'},
{'key1': 'prod', 'key2': 'testing'},
{'key1': 'testing',},
{'key1': 'prod',},
{'key1': 'testing', 'key2': 'testing'},
{'key1': 'testing',},
]
print Counter(r['key1'] for r in list_of_dicts)
I get a nice result of
Counter({'testing': 4, 'prod': 2})
However, if I change that last print to:
print Counter(r['key2'] for r in list_of_dicts)
It fails, because key2
is missing in a couple of the dictionaries.
Traceback (most recent call last):
File "test.py", line 11, in <module>
print Counter(r['key2'] for r in list_of_dicts)
File "h:\Anaconda\lib\collections.py", line 453, in __init__
self.update(iterable, **kwds)
File "h:\Anaconda\lib\collections.py", line 534, in update
for elem in iterable:
File "test.py", line 11, in <genexpr>
print Counter(r['key2'] for r in list_of_dicts)
KeyError: 'key2'
How can I use a list comprehension to count the values of key2
and not fail if a dictionary doesn't contain the key?