There is a dictionary. I can not understand how you can compare the values ​​in the dictionary and display the largest key values.

name_reiteng = {'TanyaZabrodo': 8.76, 'SanyaGypkin': 5.48, 'AnyaSymkina': 10.0, 'AntonDavidov': 4.32, 'VanyaPypkin': 6.96, 'SergeyMolodchikov': 6.76} 

In this case should output: AnyaSymkina: 10.0

in the cycle I tried to sort through: (As I think at the moment, a variable is created in a cycle that compares the value of v at this iteration and if it is larger then the new value is written to the variable, but I need the output to be keyed, how to implement correctly?) Question in a way it would be trivial, I've been sitting for about 30 minutes and I can not understand. Thank.

 for k,v in name_reiteng.items(): 

    1 answer 1

     In [101]: name_rating Out[101]: {'AntonDavidov': 4.32, 'AnyaSymkina': 10.0, 'SanyaGypkin': 5.48, 'SergeyMolodchikov': 6.76, 'TanyaZabrodo': 8.76, 'VanyaPypkin': 6.96} In [102]: from operator import itemgetter In [103]: champ = sorted(name_rating, key=itemgetter(1), reverse=True)[0] In [104]: champ Out[104]: 'AnyaSymkina' In [105]: name_rating[champ] Out[105]: 10.0 

    Or a much more elegant solution from @insolor :

     champ = max(name_rating, key=itemgetter(1)) 
    • one
      You can simply champ = max(name_rating, key=itemgetter(1)) , it is executed in linear time, unlike sort . - insolor
    • @insolor, yes, so elegant turns out, thanks! - MaxU