I have some code where I am using a list of names, and a file (eventually multiple files) of results (team, name, place). The end result I am looking for is to have each person
The problem you encounter comes from the way you create Scores
:
Scores=dict.fromkeys(lines,[])
When using dict.fromkeys
, the same value is used for all keys of the dict. Here, the same, one and only empty list is the common value for all your keys. So, whichever key you access it through, you always update the same list.
When doing Scores[a[1]]=[]
, you actually create a new, different empty list, that becomes the value for the key a[1]
only, so the problem disappears.
You could create the dict differently, using a dict comprehension:
Scores = {key: [] for key in lines} # here, a new empty list is created for each key
or use a collections.defaultdict
from collections import defaultdict
Scores = defaultdict(list)
which will automatically initialize Score['your_key']
to an empty list when needed.