Skip to main content

Posts

Showing posts with the label Pytho

Get Key Having Maximum Value In Python Dictionary

In this article, I’m going to share a scenario wherein we need to get a key from a Python dictionary, which is holding maximum value. Usually we are required to get a key having maximum key from a dictionary, which, of course, most of us can do very easily. But when it comes to the other way round, it is not that straight forward. I also found that this is one of the hottest questions these days for interviewers :) To achieve the scenario of a dictionary key having maximum value, we can go with two different ways as mentioned below: Method 1: Using itemgetter()  import operator students = {'Shweta':25,'Andy':30,'Maddy':3} v = max(students.items(),key=operator.itemgetter(1))[0] print ( v ) Method 2: Using lambda CODE import operator students = {'Shweta':25,'Andy':30,'Maddy':3} v = max(students.items(),key=lambda x:x[1])[0] print (v) You can use any of these methods and you will get the same output. I hope you enjoyed learning this co...