Python Forum

Full Version: Python Adding +1 to a list item cointained in a dict
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First, I have a list of dicts with the info of some athletes which looks like this:

 athletes = [{"name":"Robert", "country":"england", "event": "athletics men's 50000 metres", "medal":"gold"}, 
{"name":"Jan", "country":"england", "event": "athletics men's 50000 metres", "medal":"silver"}....]
I need to create a dict based on the name of Robert's country and the number of medals they (the country in a specific event) have obtained, it should look like this:

 {'canada': [0, 0, 1], 'china': [1, 0, 0]...}
The first position represents gold medals, second, silver and third bronze.

{"medal":} can have "gold", "silver", "bronze" or "na" (not applicable)

I've tried doing this:

 
def funct(athletes:list, event:str):
    import copy
    list1 = [0, 0, 0]
    dicc = {}
    for i in athletes:
        if i["event"] == event and i["medal"] != 'na':
            dicc[i["country"]] = copy.deepcopy(list1)
        for country in dicc.keys():
            if country == i["country"] and i["medal"] == "gold":
                dicc[country][0] +=1
            elif country == i["country"] and i["medal"] == "silver":
                dicc[country][1] +=1
            elif country == i["country"] and i["medal"] == "bronze":
                dicc[country][2] +=1
return dicc

print(funct(athletes, "athletics men's 50000 metres"))
It works, but I'm just getting the info of the last athlete of that country (using the list from above):

{'england': [0, 1, 0]} 
When I should be getting (expected result):

{'england': [, 1, 0]} 
What should I do to fix this?
Indenting got messed up in your post. I think. As posted the function doesn't even return anything.

From what I can glean from the code I see why you only get the last athlete. Each time you find an athlete that medals in an event you reset the medal count for his country. You probably want to do something more like this:
medals = empty dictionary
for each entry in athletes:
    if entry.event == event
        if entry.country not in medals
             add country to medals
        if entry.medal is gold
             etc...
     return medals
Why do you have an import inside your function? That is an odd place to have an import and you don't need the deep copy for a list of ints.

And don't type ugly code like this:
dicc[i["country"]] = copy.deepcopy(list1)
if country == i["country"] and i["medal"] == "gold":
    dicc[country][0] +=1
elif country == i["country"] and i["medal"] == "silver":
    dicc[country][1] +=1
elif country == i["country"] and i["medal"] == "bronze":
    dicc[country][2] +=1
Ugh! No matter how elegant the algorithm it is hidden by hideous punctuation. Code that looks good is easier to read and understand. Use meaningful names and local variables
medals = {}
for athlete in athletes: 
    if athlete['event'] == event:
        country = athlete['country']
        medal = athlete['medal']
        if not country in medals:  # Add country to medal list
            medals[country] = [0, 0, 0]