Get key by value in dictionary

后端 未结 30 2340
北恋
北恋 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 07:14

    for name in mydict:
        if mydict[name] == search_age:
            print(name) 
            #or do something else with it. 
            #if in a function append to a temporary list, 
            #then after the loop return the list
    

提交回复
热议问题