python: use the setdefault method on dict

It allows more concise code and skips some key lookups.
This commit is contained in:
Ben Boeckel
2015-10-15 09:18:16 -04:00
parent f76b3890ff
commit 9d90cb6962
3 changed files with 7 additions and 16 deletions

View File

@@ -87,10 +87,7 @@ def index_by(objects, *funcs):
result = {}
for o in objects:
key = f(o)
if key not in result:
result[key] = [o]
else:
result[key].append(o)
result.setdefault(key, []).append(o)
for key, objects in result.items():
result[key] = index_by(objects, *funcs[1:])