check if key in dictionary python count 2b1 add if it is

Solutions on MaxInterview for check if key in dictionary python count 2b1 add if it is by the best coders in the world

showing results for - "check if key in dictionary python count 2b1 add if it is"
Lennard
02 May 2018
1from collections import defaultdict
2dct = defaultdict(int)
3
4for key in data:
5  dct[key] += 1
6
7# defaultdict(<type 'int'>, {'a': 4, 'c': 3, 'b': 2, 'd': 1})