Skip to content

Home

Invert dictionary

Inverts a dictionary with unique hashable values.

def invert_dictionary(obj):
  return { value: key for key, value in obj.items() }

ages = {
  'Peter': 10,
  'Isabel': 11,
  'Anna': 9,
}
invert_dictionary(ages) # { 10: 'Peter', 11: 'Isabel', 9: 'Anna' }

More like this

Start typing a keyphrase to see matching snippets.