Python | Get key with maximum value in Dictionary

Python | Get key with maximum value in Dictionary

To get the key associated with the maximum value in a dictionary, you can use the max function along with a lambda function for the key argument.

Here's how you can do this:

def get_key_with_max_val(dictionary): return max(dictionary, key=dictionary.get) # Example data = { 'a': 10, 'b': 25, 'c': 7 } print(get_key_with_max_val(data)) # Outputs: 'b' 

In this example, max(dictionary, key=dictionary.get) will return the key associated with the highest value in the dictionary. The dictionary.get function returns the value for a given key, and the key argument of the max function allows us to specify a custom function (in this case, dictionary.get) to determine the maximum value.


More Tags

h5py word-frequency android-adapterview pnp-framework custom-taxonomy graphviz cloudera-cdh postman-collection-runner binary-tree manager-app

More Programming Guides

Other Guides

More Programming Examples