Created
September 16, 2015 08:49
-
-
Save harunyasar/6fba10d9712e02609061 to your computer and use it in GitHub Desktop.
Get the dict key by value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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