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

Get max value from nested dictionary return both keys if equal

I have following dictionary

  {
    "match_1":{
        "home_team":2,
        "away_team":1
    },
    "match_2":{
        "home_team":1,
        "away_team":2
    },
    "match_3":{
        "home_team":1,
        "away_team":4}


}

I want to sum home team and away team goals and find which team won in aggregate
if both have same score result is draw.
I tried converting to tuple and used max with lambda but it fails when result score is same.

required output for current question: winner! away team

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

if same goals in all matches in aggregate: Draw

>Solution :

Use this tricky way

d={
    "match_1":{
        "home_team":2,
        "away_team":1
    },
    "match_2":{
        "home_team":1,
        "away_team":2
    },
    "match_3":{
        "home_team":1,
        "away_team":4}


}
score = [i.values() for i in d.values()]
t1,t2 = map(sum,zip(*score))
[["Away team","Home team"][t1>t2], "Draw"][t1==t2]
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