I have a list of dicts in python, for example:
[
{
"25-34": {
"Clicks": 10
},
"45-54": {
"Clicks": 2
},
},
{
"25-34": {
"Clicks": 20
},
"45-54": {
"Clicks": 10
},
}
]
how can i get the sum of the keys in each announcer of the list so i have:
{
"25-34": {
"Clicks": 30
},
"45-54": {
"Clicks": 12
},
}
I tried to use Counter()
, but it works easily when the internal list is dicts
flat, but with nested dicts, as mentioned above, gives this error:
/usr/lib/python2.7/collections.pyc in update(self, iterable, **kwds)
524 self_get = self.get
525 for elem, count in iterable.iteritems():
527 else:
528 super(Counter, self).update(iterable)
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
How can I achieve the summation as described above.
NOTE : I added clicks
for sample only. nested voice recorders can have any keys, another example to make it more clear:
[
{
"25-34": {
"Clicks": 10,
"Visits": 1
},
"45-54": {
"Clicks": 2,
"Visits": 2
},
},
{
"25-34": {
"Clicks": 20,
"Visits": 3
},
"45-54": {
"Clicks": 10,
"Visits": 4
},
}
]
output:
{
"25-34": {
"Clicks": 30,
"Visits": 4
},
"45-54": {
"Clicks": 12,
"Visits": 6
},
}