I am trying to build a function to merge two lists into a dictionary, but the "key" list contains duplicates. I would like to sum the values based on the key. The lists could have any number of values (but each list would have the same number).
I have tried out many different codes that I have found through Googling, but none of them were for this situation, and none worked quite right. I'm just getting started with learning Python, so I'm sure there's something I'm missing!
my_dict={}
def merge_lists(my_dict):
newlist={k: sum(i[k] for i in my_dict) for k in my_dict[0]}
return newlist
If I start with these lists: key_list=[1, 5, 3, 8, 5, 8, 3], value_list=[2, 3, 7, 1, 4, 9, 2]
I'm looking for this result: {'1':2, '3':9, '5':7, '8':10}