Files
30-seconds-of-code/snippets/collect_dictionary.md
2020-10-10 23:16:53 +02:00

628 B

title, tags
title tags
collect_dictionary dictionary,intermediate

Inverts a dictionary with non-unique hashable values.

  • Use dictionary.items() in combination with a loop to map the values of the dictionary to keys using collections.defaultdict, list() and append() to create a list for each one.
from collections import defaultdict

def collect_dictionary(obj):
  inv_obj = defaultdict(list)
  for key, value in obj.items():
    inv_obj[value].append(key)
  return inv_obj
ages = {
  "Peter": 10,
  "Isabel": 10,
  "Anna": 9,
}
collect_dictionary(ages) # { 10: ["Peter", "Isabel"], 9: ["Anna"] }