Julio S.
Julio S.

Reputation: 970

Python (lists): how to group duplicate labels, summing up their respective values

I have 2 lists in Python 3.7: one for labels and other for values. Each of the labels contains one respective value and there are duplicated labels.

How can I remove these duplicates in the list of labels, summing up all their respective values?

Example of what I have:

labels = ['05/Feb/2019', '05/Feb/2019', '05/Feb/2019', '01/Feb/2019']
values = ['2', '5', '7', '4'] 

What I need is:

labels = ['05/Feb/2019', '01/Feb/2019']
values = ['14', '4']

Upvotes: 0

Views: 446

Answers (3)

Mykola Zotko
Mykola Zotko

Reputation: 17834

You can use Counter:

from collections import Counter

labels = ['05/Feb/2019', '05/Feb/2019', '05/Feb/2019', '01/Feb/2019']
values = ['2', '5', '7', '4']

c = Counter(l for l, v in zip(labels, values)
              for _ in range(int(v)))
# Counter({'05/Feb/2019': 14, '01/Feb/2019': 4})

labels = list(c)
# ['05/Feb/2019', '01/Feb/2019']

values = list(c.values())
# [14, 4]

Upvotes: 1

blue note
blue note

Reputation: 29081

An easy way would be to create a default dict and then unwrap it

from collections import defaultdict

result = defaultdict(float)

for label, value in zip(labels, values):
    result[label] += float(value)

labels, values = zip(*result.items())

Upvotes: 2

yatu
yatu

Reputation: 88236

You could use itertools.groupby to group both lists zipped together, and add the elements in values with common labels:

from itertools import groupby
from operator import itemgetter

z = zip(labels,values)
r = [(k, sum(int(i[1]) for i in v)) for k,v in groupby(z, key=itemgetter(0))]
labels, values = map(list, zip(*r))

print(labels)
# ['05/Feb/2019', '01/Feb/2019']

print(values)
# [14, 4]

Upvotes: 2

Related Questions