Grouping Calcuted value schedule List

I want to group the Calculated value list according to level and have sum of that parameter.I was able to get the calculated value from the list but could not able to group it and sum it up


so i have one list [a,a,b,b,c,c,d] and another [0,0,2,3,12,3,4] i want the result to be [[a,0,0],[b,2,3],[c,12,3],[d,4]]

what i really wants

and my dynamo script is

i got it working with def combine_lists(list1, list2):
result =
occurrences = defaultdict(list)

# Group the occurrences of each element in list1
for elem, num in zip(list1, list2):
    occurrences[elem].append(num)

# Create the result list with the grouped elements and their occurrences
for elem, nums in occurrences.items():
    result.append([elem] + nums)

return result
1 Like

Hi @saurabh.j,

You can try something like this in case if you need to preserve the list order. This options doesn’t merge 0,0,5 under a.

from itertools import groupby
OUT = [([key] + [x[1] for x in group]) for key, group in groupby(zip(IN[0], IN[1]), key=lambda x: x[0])]

1 Like

thank you this works better

1 Like