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

How to find min and max of dictionary where 2 or more keys have the same value

I would like to find min and max of dictionary where 2 or more keys have the same value.

The following dict:

{0: 110001,
 1: 123716,
 2: 110001,
 3: 123349,
 4: 110001,
 5: 140000,
 6: 140000,
 7: 123601,
 8: 122691,
 9: 123767}

At the end I would like to print sth like that:

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

"5 and 6- max values"
"0 and 2 and 4- min values"

This is what I’ve tried:

print("Min value occurs for: ", min(dict_counter, key=dict_counter.get))

>Solution :

You could extract the values and get the min/max, and then get the items and filter by those values:

d = {0: 110001, 1: 123716, 2: 110001,3: 123349, 4: 110001, 5: 140000, 6: 140000, 7: 123601, 8: 122691, 9: 123767}

low = min(d.values())
high = max(d.values())
print(f"low {low} found in {[k for k, v in d.items() if v == low]}")
print(f"high {high} found in {[k for k, v in d.items() if v == high]}")
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