Try it here
Subscribe
Python max , dict,get and dict.values()

Python - Find Max value in Dictionary

python_-_find_max_value_in_dictionary

Finding the max value in a dictionary finds either the key associated with the maximum value or the value itself. For example, in {"a": 1, "b": 2, "c": 3}, the key of the largest value is "c" and the largest value is 3.

KEY WITH THE MAX VALUE

USE max() AND dict.get TO FIND THE KEY WITH THE MAX VALUE IN A DICTIONARY

Call max(iterable, key=dict.get) with the same dictionary as both iterable and dict to find the key paired with the max value.

>>> a_dict = {"a": 1, "b": 2, "c": 3}
>>> max(a_dict, key=a_dict.get)
'c'
>>> 

MAX VALUE IN A DICTIONARY

USE max() AND dict.values() TO FIND THE MAX VALUE IN A DICTIONARY

>>> max(a_dict.values())  # a_dict.values() returns a list
3
>>> 

Writer profile pic

Prakash on Sep 20, 2020 at 02:09 am


This article is contributed by Prakash. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.