python - Add dictionary values where keys match -


i have list of dictionaries following:

a=[{'a':1},{'b':2},{'c':3},{'a':-2},{'b':4}] 

i need loop through list , add values keys match. result should this:

a=[{'a':-1},{'b':6},{'c':3}] 

help doing appreciated.

i able implement like

a = [{'a':1},{'b':2},{'c':3},{'a':-2},{'b':4}] b = {}  x in a:     k,v = x.popitem()      # check if key in our output dict     if k in b.keys():         b[k] += v      # if not, create     else:         b[k] = v 

which outputs

b = {'a': -1, 'b': 6, 'c': 3} 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -