List.Scan used for cumulative sum does not complete and returns null

I am starting with a list of distances between points and trying to create a list of cumulative sum of their distances. The calculation I have implemented is unable to finish and begins returning null in the middle of the list. For some inputs it will fully complete and for some it will begin returning null at different points in the list. I am trying to figure out if it is some feature of my list (too large) or something else. I used the Math.Round thinking it would reduce the processing load but it doesn’t seem to make a difference.

I’ve also found that if I restart dynamo and run it again, it doesn’t fail in the exact same point every time and will sometimes even complete successfully without null values.

I had found that List.Scan was suggested for calculating cumsums here: Cumulative sum of list in dynamo - #3 by jostein_olsen

That’s quite a lot of calculations to perform iteratively. My machine actually had a little trouble running List.Scan as well as my usual method of summing an expanded list on that many elements. Python is going to perform much better in this situation.

out = []
sum = 0
for n in IN[0]:
	sum = sum + n
        out.append(sum)
OUT = out
2 Likes

other workaround, this one the first item of the list is 0:
Making a list by summing values - #7 by RubenVivancos

A Design Script implementation of @Nick_Boyts’ suggestion in a Code Block Node
sum

toSum;
[Imperative]
{
	sum = 0;
	for (i in toSum)
		sum = i + sum;
	return sum;
};
1 Like

@ba367 See if this helps:
image

OUT = []
sum = 0
for num in IN[0]:
    sum += num
    OUT.append(sum)
2 Likes

@AmolShah’s code in DS
sum_2021-10-02_02-03-31

toSum;
[Imperative]
{
	num = [];
	sum = 0;
	for (i in toSum)
    {
    	sum = i + sum;
    	num = List.Flatten([num,sum]);
    }
    return num;
};
4 Likes