python dictionary count values per key 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(, {'a': 4, 'c': 3, 'b': 2, 'd': 1})
Example 2: get number of key in dictionary python
#Call len(obj) with a dictionary as obj to count the number
#of key-value pairs in the dictionary.
a_dictionary = {"a": 1, "b": 2}
print(len(a_dictionary))
OUTPUT:
2
Example 3: python counting dictionary
counts = dict()
for i in items:
counts[i] = counts.get(i, 0) + 1