Skip to content

Instantly share code, notes, and snippets.

@kentquirk
Created September 10, 2016 17:21
Show Gist options
  • Save kentquirk/44da7d96a9763c16f2fe3525e85bf595 to your computer and use it in GitHub Desktop.
Save kentquirk/44da7d96a9763c16f2fe3525e85bf595 to your computer and use it in GitHub Desktop.
Try using exceptions
output = dict()
for element in input:
try:
output[element["owner"]].append(element["pet"])
except KeyError:
output[element["owner"]] = [element["pet"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment