# How to get the sums of unique values with respective occurrence count from a list in Python?

With a list like this:

``````[1,1,1,2,2,3,3,3]
``````

I would like to get the sums of each unique number `[1,2,3]` which is `[3, 4, 9]`. Using an approach from the related post How to get unique values with respective occurrence count from a list in Python? I'm able to get the count of the occurence of each uniqe number using:

``````L = [1,1,1,2,2,3,3,3]
uniq, counts = np.unique(L, return_counts=True)
counts
``````

Which gives the output `[3, 2, 3]`. With this, I'm able to get what I'm looking for using a cumbersome approach with an enumerated `For Loop` and some rather cryptic conditions:

``````L = [1,1,1,2,2,3,3,3]
elements = [3,2,3]

sums = []
index = 0
for i, e in enumerate(elements):
if i == 0:
sums.append(sum(L[0:e]))
index = index + e
else:
sums.append(sum(L[index:index + e]))
index = index + e
sums
``````

Which gives the desired output `[3, 4, 9]`. Does anyone know if it's possible to do the same thing a bit more elegantly?