python defaultdict

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

showing results for - "python defaultdict"
Edna
22 Jun 2020
1>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
2>>> d = defaultdict(list)
3>>> for k, v in s:
4...     d[k].append(v)
5...
6>>> d.items()
7[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
8
Chiara
10 Jan 2021
1>>> from collections import defaultdict
2>>> ice_cream = defaultdict(lambda: 'Vanilla')
3>>>
4>>> ice_cream = defaultdict(lambda: 'Vanilla')
5>>> ice_cream['Sarah'] = 'Chunky Monkey'
6>>> ice_cream['Abdul'] = 'Butter Pecan'
7>>> print ice_cream['Sarah']
8Chunky Monkey
9>>> print ice_cream['Joe']
10Vanilla
11>>>
Emilie
19 Jun 2017
1d = defaultdict(lambda:1)
Maximiliano
01 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...
7defaul