Find similar items in list, add them and then add the result to another list

I have a list that I'm trying to find similar items, add them to a temp list, find the sum of the similar items, and then add that result to another list. The result I get is [15,6] for the following but I was expecting to get [20,12,16]? I cant seem to include all the similar numbers and cant seem to get the for loop to include the last number of the similar item. Please take a look at the following code so far, any comments would help?

start_list = [5,5,5,5,6,6,8,8]
temp_list = []
final_list = []

for i in range(len(start_list )-1):
    if start_list [i] == start_list [i+1]:
        temp_list.append(start_list [i])
    else:
        total = sum(temp_list)
        final_list .append(total)
        temp_list = []

print(final_list)


Read more here: https://stackoverflow.com/questions/68461119/find-similar-items-in-list-add-them-and-then-add-the-result-to-another-list

Content Attribution

This content was originally published by Musclemania05 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: