def group_by(key, items):
    result = defaultdict(list)
for item in items:
    result[key(item)].append(item)
return result
group_by(len, ['here', 'are', 'some', 'words'])
The price of using infinite_defaultdict