In python 2.2:
stat2 = {} for s in stat: for c in s.values(): for k, v in c.items(): if k in stat2: stat2[k] += v else: stat2[k] = v
If you used python> 2.4, you could just use defaultdict:
from collections import defaultdict stat2 = defaultdict(list) for s in stat: for c in s.values(): for k, v in c.items(): stat2[k] += v stat2 defaultdict(<type 'list'>, {'europe': ['germany', 'england', 'french', 'netherland'], 'asian': ['japan', 'china']})
And if you really wanted to:
{'state': [dict(stat2)]} {'state': [{'asian': ['japan', 'china'], 'europe': ['germany', 'england', 'french', 'netherland']}]}
Consider updating your version of python, 2.2 was released in 2001 ...!
source share