How to get key by passing value from dictionary in python? [duplicate]
![How to get key by passing value from dictionary in python? [duplicate]](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F80wy3gkl%2Fproduction%2F54e30bbcb677f725db7afda2ea2f51db97c37e46-1201x631.png%3Fh%3D1000&w=3840&q=75)
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