python dictionary count code example
Example 1: check if key in dictionary python count +1 add if it is
from collections import defaultdict
dct = defaultdict(int)
for key in data:
dct[key] += 1
# defaultdict(<type 'int'>, {'a': 4, 'c': 3, 'b': 2, 'd': 1})
Example 2: how many keys in a dictionary python
> a = {'foo':42, 'bar':69}
> len(a)
2
Example 3: python counting dictionary
counts = dict()
for i in items:
counts[i] = counts.get(i, 0) + 1