May 2018
Intermediate to advanced
380 pages
9h 37m
English
The following examples are taken from the Python documentation at https://docs.python.org/3/library/collections.html#collections.defaultdict:
>>> from collections import defaultdict >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = defaultdict(list) >>> for k, v in s: ... d[k].append(v) ... >>> sorted(d.items()) [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]