Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the key for second maximum value from a dictionary, Python

I am doing it this way but if there is any easy/efficient way of doing it

def get_second_max(dic):
    count = 0
    ind =0
    vals = list(dic.values())
    for val in vals:
        count = 0
        for value in vals:
            if value > val:
                count += 1
            if count > 2:
                break
        if count == 1:
            ind = vals.index(val)
    return list(dic.keys())[ind]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sort the dict keys by their value and get the second to last item:

sorted(dic, key=dic.get)[-2] 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading