defaultdict

Solutions on MaxInterview for defaultdict by the best coders in the world

showing results for - " defaultdict"
Christopher
18 Jul 2017
1>>> from collections import defaultdict
2>>> food_list = 'spam spam spam spam spam spam eggs spam'.split()
3>>> food_count = defaultdict(int) # default value of int is 0
4>>> for food in food_list:
5...     food_count[food] += 1 # increment element's value by 1
6...
7defaultdict(<type 'int'>, {'eggs': 1, 'spam': 7})
8>>>