Get key by value in dictionary

后端 未结 30 2341
北恋
北恋 2020-11-21 06:28

I made a function which will look up ages in a Dictionary and show the matching name:

dictionary = {\'george\' : 16, \'amber\' : 19}
search_age          


        
30条回答
  •  被撕碎了的回忆
    2020-11-21 06:56

    There is no easy way to find a key in a list by 'looking up' the value. However, if you know the value, iterating through the keys, you can look up values in the dictionary by the element. If D[element] where D is a dictionary object, is equal to the key you're trying to look up, you can execute some code.

    D = {'Ali': 20, 'Marina': 12, 'George':16}
    age = int(input('enter age:\t'))  
    for element in D.keys():
        if D[element] == age:
            print(element)
    

提交回复
热议问题