Skip to content

Instantly share code, notes, and snippets.

View gdementen's full-sized avatar

Gaëtan de Menten gdementen

  • Federal Planning Bureau
  • Belgium
View GitHub Profile
@gdementen
gdementen / petowner_comprehension1.py
Last active September 16, 2016 19:36 — forked from kentquirk/petowner_comprehension1.py
Set up output first
# 1) like others pointed out, you should use a defaultdict in this case
# 2) otherwise use setdefault
output = {}
for element in elements:
output.setdefault(element["owner"], []).append(element["pet"])
# 3) if, for some reason, this does not work in your case, use a dict comprehension (if available for your version of Python, 2.7+ I think)
output = {e["owner"]: [] for e in elements}
for element in elements: