Is there a way to aggregate the value of each entry of a list using Counter
, and not just count the number of time an entry is present ?
For example, if I have a list
from collections import Counter
lst = [
['John', 1],
['James', 2],
['John', 3],
['Andy', 1]
]
and I do
total = Counter(name for name, num in lst)
print(total)
The output will be
Counter({'John': 2, 'James': 1, 'Andy': 1})
Which is expected. I was just wondering if there's a way to obtain
Counter({'John': 4, 'James': 2, 'Andy': 1})
I know there are many ways to do it, but I'm looking for a way to do it with Counter
.