How to get key by passing value from dictionary in python? [duplicate]

How to get key by passing value from dictionary in python? [duplicate]

The following line gives me the maximum value from the dictionary. But I need to access the key with respect to the max(value) of the dictionary. Help !

print("The winner is",max(biddinglist.values()))

Answer

You can use the max function with the key parameter. Note that this is equivalent to looping through all of the key / value items and is therefore O(n)

max(biddinglist.items(), key=lambda i: i[1])[0]

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles