I have a list as follows,
flat_list = ['hello,5', 'mellow,4', 'mellow,2', 'yellow,2', 'yellow,7', 'hello,7', 'mellow,7', 'hello,7']
I would like to get the sum of the values if they share the same word, so the output should be,
desired output:
l = [('hello',19), ('yellow', 9), ('mellow',13)]
so far, I have tried the following,
new_list = [v.split(',') for v in flat_list]
d = {}
for key, value in new_list:
if key not in d.keys():
d[key] = [key]
d[key].append(value)
# getting rid of the first key in value lists
val = [val.pop(0) for k,val in d.items()]
# summing up the values
va = [sum([int(x) for x in va]) for ka,va in d.items()]
however for some reason the last sum up does not work and i do not get my desired output
d[w] = d.get(w, 0) + int(n)
simpler. – Ondometer