How to find key by value in dictionary with Python

2 Answers

0 votes
dict = {'key1':34, 'key2':89, 'key3':12, 'key4':72, 'key5':90}

list_of_keys = list(dict.keys())
list_of_values = list(dict.values())
 
index = list_of_values.index(12)
print(list_of_keys[index])

    
    
    
'''
run:

key3

'''

 



answered Apr 11, 2021 by avibootz
0 votes
def get_key(val):
    for key, value in dict.items():
         if val == value:
             return key
 
    return "value not exist"
    
dict = {'key1':34, 'key2':89, 'key3':12, 'key4':72, 'key5':90}

print(get_key(12))

    
    
    
'''
run:

key3

'''

 



answered Apr 11, 2021 by avibootz

Related questions

1 answer 142 views
4 answers 179 views
4 answers 206 views
1 answer 118 views
2 answers 150 views
...