> Another way of avoiding KeyError is using dict.get(val, default_val)
Except that
my_dict = {}
my_dict[val] = my_dict.get(val, []).append('foo')
doesn't work because list.append() doesn't return anything, while my_dict = defaultdict(list)
my_dict[val].append('foo')
does the right thing.Defaultdict is sugar for dict.setdefault, not dict.get.