You need to convert your input dictionaries to (title: count)
pairs, using them as keys and values in a Counter
; then after summing, you can convert these back to your old format:
from collections import Counter
summed = sum((Counter({elem['title']: elem['views']}) for elem in a + b), Counter())
c = [{'title': title, 'views': counts} for title, counts in summed.items()]
Demo:
>>> from collections import Counter
>>> a = [{'title': 'Learning How to Program', 'views': 1},
... {'title': 'Mastering Programming', 'views': 3}]
>>> b = [{'title': 'Learning How to Program', 'views': 7},
... {'title': 'Mastering Programming', 'views': 2},
... {'title': 'Programming Fundamentals', 'views': 1}]
>>> summed = sum((Counter({elem['title']: elem['views']}) for elem in a + b), Counter())
>>> summed
Counter({'Learning How to Program': 8, 'Mastering Programming': 5, 'Programming Fundamentals': 1})
>>> [{'title': title, 'views': counts} for title, counts in summed.items()]
[{'views': 8, 'title': 'Learning How to Program'}, {'views': 5, 'title': 'Mastering Programming'}, {'views': 1, 'title': 'Programming Fundamentals'}]
The goal here is to have a unique identifier per count. If your dictionaries are more complex, you either need to convert the whole dictionary (minus the count) to a unique identifier, or pick one of the values from the dictionary to be that identifier. Then sum the view counts per identifier.
From your updated example, the URL would be a good identifier. That'd let you collect the view count in place:
per_url = {}
for entry in a + b:
key = entry['url']
if key not in per_url:
per_url[key] = entry.copy()
else:
per_url[key]['views'] += entry['views']
c = per_url.values() # use list(per_url.values()) on Python 3
This simply uses the dictionaries themselves (or at least a copy of the first one encountered) to sum the view counts:
>>> from pprint import pprint
>>> a = [{'title': 'Learning How to Program', 'views': 1,'url': '/4XvR', 'slug': 'learning-how-to-program'},
... {'title': 'Mastering Programming', 'views': 3,'url': '/7XqR', 'slug': 'mastering-programming'}]
>>> b = [{'title': 'Learning How to Program', 'views': 7,'url': '/4XvR', 'slug': 'learning-how-to-program'},
... {'title': 'Mastering Programming', 'views': 2,'url': '/7XqR', 'slug': 'mastering-programming'},
... {'title': 'Programming Fundamentals', 'views': 1,'url': '/93hB', 'slug': 'programming-fundamentals'}]
>>> per_url = {}
>>> for entry in a + b:
... key = entry['url']
... if key not in per_url:
... per_url[key] = entry.copy()
... else:
... per_url[key]['views'] += entry['views']
...
>>> per_url
{'/93hB': {'url': '/93hB', 'title': 'Programming Fundamentals', 'slug': 'programming-fundamentals', 'views': 1}, '/4XvR': {'url': '/4XvR', 'title': 'Learning How to Program', 'slug': 'learning-how-to-program', 'views': 8}, '/7XqR': {'url': '/7XqR', 'title': 'Mastering Programming', 'slug': 'mastering-programming', 'views': 5}}
>>> pprint(per_url.values())
[{'slug': 'programming-fundamentals',
'title': 'Programming Fundamentals',
'url': '/93hB',
'views': 1},
{'slug': 'learning-how-to-program',
'title': 'Learning How to Program',
'url': '/4XvR',
'views': 8},
{'slug': 'mastering-programming',
'title': 'Mastering Programming',
'url': '/7XqR',
'views': 5}]