Python Forum
Python Adding +1 to a list item cointained in a dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Adding +1 to a list item cointained in a dict
#1
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?
Reply


Messages In This Thread
Python Adding +1 to a list item cointained in a dict - by ElReyZero - Apr-30-2020, 01:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a dict code in python ariellea88 4 1,041 Oct-31-2023, 08:45 AM
Last Post: buran
  help with adding duplicates elements together in a list 2ECC3O 5 2,080 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  change item in column based on the item before[solved] amdi40 4 1,788 Jan-11-2022, 04:50 PM
Last Post: amdi40
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,937 May-14-2021, 07:14 AM
Last Post: perfringo
  List index out of range when turning CSV into dict ranbarr 15 6,498 May-12-2021, 10:38 AM
Last Post: ranbarr
  Dict from list - HELP! PLEASE! cherry_cherry 16 5,616 Apr-09-2020, 04:01 AM
Last Post: cherry_cherry
  adding parts of a list Eric7Giants 4 2,768 Nov-17-2019, 05:53 PM
Last Post: buran
  Adding values to list and pickling mefiak 2 2,840 May-31-2018, 08:57 AM
Last Post: mefiak
  adding a number to the list atux_null 4 3,889 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk
  need help removing an item from a list jhenry 4 4,213 Oct-13-2017, 08:15 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020