Skip to content

Instantly share code, notes, and snippets.

@harunyasar
Created September 16, 2015 08:49
Show Gist options
  • Save harunyasar/6fba10d9712e02609061 to your computer and use it in GitHub Desktop.
Save harunyasar/6fba10d9712e02609061 to your computer and use it in GitHub Desktop.
Get the dict key by value
words = {
'lorem': 1,
'ipsum': 2,
'dolor': 3,
'sit': 4,
'amet': 5
}
def get_the_key(my_dict, value):
return my_dict.keys()[my_dict.values().index(value)]
print get_the_key(words, 1) # 'lorem'
print get_the_key(words, 2) # 'ipsum'
print get_the_key(words, 3) # 'dolor'
print get_the_key(words, 4) # 'sit'
print get_the_key(words, 5) # 'amet'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment