Dictionaries: obtain the key from a value

Hi there,

if I have a dictionary like so:

dictionary = {1: 10, 2: 20, 3: 30}

I want to return the key that has the value 20 in dictionary. What’s the syntax?


By looping over a dictionary, we can get the key and value, then use a condition to compare the value with 20 (using conditions), if this condition is true, return the key

@stetim94 's way is the only way I can think of doing it. Keep in mind, though - dictionary values don’t have to be unique, so what happens if your target value (e.g. 20) is in more than one key? :slight_smile: