Reputation: 2127
I am trying to output the name of the pet which corresponds to the highest value:
import operator
pets = {"Dog":3, "Cat":5, "Rabbit":0}
sorted_pets = sorted(pets.items(), key=operator.itemgetter(1), reverse = True)
print (sorted_pets[0])
The code above outputs ['Cat', 5]
. How can I change it so it just outputs Cat
?
Thanks.
Upvotes: 2
Views: 76
Reputation: 362478
Possibly a more pythonic alternative:
>>> max(pets, key=pets.get)
'Cat'
Upvotes: 7