Juan Romero
Juan Romero

Reputation: 17

Comparing dicts of lists made from dicts Python

Sorry for the confusion this problem can cause you.

I have a dict which keys are country names and values that are lists of dicts, see example below:

{'spain': [{'gold': 3}, {'silver': 1}, {'bronze': 0}], 'colombia': [{'gold': 2}, {'silver': 0}, {'bronze': 0}]}

I need to compare them and get the one that has more gold medals, but I don't know any way of doing it.

PS: I need to return the country with the most medals in the same way as above:

{'country':[{'gold':3}, {'silver':3}, {'bronze':3}]}

Edit: Clarification

Upvotes: 0

Views: 34

Answers (1)

yatu
yatu

Reputation: 88236

You could use max with a custom key function:

dict([max(d.items(), key=lambda x: x[1][0]['gold'])])
# {'spain': [{'gold': 3}, {'silver': 1}, {'bronze': 0}]}

Upvotes: 1

Related Questions