Cumulative sum of list in dynamo

Hi . I am trying to make a cumulative sum of the list. For example i have a list with numbers 1, 2, 3, 4 and i want to create new list that would be 1, 3, 6, 10 … i searched the internet for python script but nothing has worked in dynamo. <span style=“color: #555555;”> I would like to just set one variable, connect it to the list, and then new variable of new list that i could drag and work with it</span>

I found this on the internet but i cant implement it in dynamo:

http://www.sohua.xyz/questions/348802/how-to-find-the-cumulative-sum-of-numbers-in-a-list
<pre class=“prettyprint prettyprinted”>

That should be pretty straight forward:

 

 

 

 

First we create multiple ranges - the first item, first and second, first to third, etc. Then we sum each new list an we get the final result.

2 Likes

There is also an OOTB node which works nicely, List.Scan. Also a Lunchbox node which does the same, but without the seed input:

3 Likes

Thank you guys you helped me. This is the result

2 Likes

I am assuming none of this is possible anymore? I can’t get lunchbox nodes in Dynamo for C3D 2024 and the GetKeys node is long gone, am I right?

You could try this:

import clr

lst = IN[0]

accum = 0
output = []

for i in lst:
    accum = accum+i
    output.append(accum)

OUT = output
1 Like

This bit of design script should also work if you don’t want to go the Python route.

count = Lost.Count(values);
drops = -count+1..0;
sets = List.Drop(values, drops@L1<1>);
sums = Math.Sum(sets);
1 Like

Hi, you did this from a phone :wink:

count = List.Count(values);
drops = -count+1..0;
sets = List.DropItems(values, drops@L1<1>);
sums = Math.Sum(sets);

cordially
christian.stan

Yes, and Lost is not the same as List. :laughing:

2 Likes

It wasn’t meant to be, but I often get lost in the lists.
:laughing: :wink:
(Master Capello comes out of this body, if you are old and French you will understand)

cordially
christian.stan

1 Like

Im old but not french probably thats why i dont understand :grinning:

1 Like

Thank you both! What would be the advantage of using design script over python? Is it just that the graph doesn’t need the package downloaded?

Also, after some digging, here is what I’ve stolen from the forum to accomplish the task within sublists.

OUT = [[sum(i[:j]) for j in range(1,len(i)+1)] for i in IN[0]]

1 Like

Design script will be faster in a player context, as it doesn’t have to spin up the Python engine. That said you’re not likely to see any issues with speed using either setup. :slight_smile:

3 Likes