I'm looping through a list and trying to create a dictionary with key/pair
values, the problem is that as I'm looping through the list, I may find duplicate instances of a key, in which case I'd like to add another pair. I know what I need is some sort of list of lists, and while I got it working for instances where there are two values of a pair for a key, it fails when I get to 3 or more.
This is my current code:
team2goals = {}
<loop through list>
timestamp = xValue
if name in team2goals:
temp = team2goals[name]
temp = {temp,timestamp}
team2goals[name] = temp
else:
team2goals[name] = timestamp
Where team2goals
is a dictionary of <str>,<str>
. It checks to see if an instance of the key (name
) exists, and if it does it stores the current key value and creates a dictionary of the values, otherwise it just adds a new key/pair value.
I tried something like:
tempT = team1goals[name]
temp = []
temp.append(tempT)
temp.append(timestamp)
team1goals[name] = temp
But that just seemed to start nesting dictionaries i.e. [[timestamp,timestamp2],timestamp3]
Is there a way to do what I'm trying?