deafultdict python code example
Example 1: defaultdict in python
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
Example 2: defaultdict count key value pairs
d = {'a': 1, 'b': 1, 'c': 3, 'd': 4}
>>> sum(d.values())