What is the easiest way to do this without using defaultdict? If too cumbersome to do this with core python functionality, then defaultdict is okay.
I have a list of keys as a list.
I have a short loop to create a few lists with values, of which I want to map to the keys in the dict.
So, in the end I will get multiple values per key. Then print.
This is what I have so far...
Keys = ['A','B','C'] #the keys that will be mapped to all values start here in a list
Values = [] #starting with empty values
Dict = dict(zip(Keys, Values)) #starting with a dict of keys mapped to empty values
x=0
while x < 5:
x += 1
MoreValues = [1+x,2+x,3] #adding x only to give some different values
Dict = dict(zip(Keys, MoreValues))
print Dict
Output with this code is:
{'A': 2, 'C': 3, 'B': 3}
{'A': 3, 'C': 3, 'B': 4}
{'A': 4, 'C': 3, 'B': 5}
{'A': 5, 'C': 3, 'B': 6}
{'A': 6, 'C': 3, 'B': 7}
Another way to look at the question, is how can these resulting dictionaries be merged together so the keys of A, B, C are mapped to those multiple values, given this setup with a for loop to generate the list of values.
Output should be:
{'A': [2, 3, 4, 5, 6], 'C': [3, 3, 3, 3, 3], 'B': [3, 4, 5, 6, 7]}