Help with summing values in sublists including empty lists without math.sum removing empty lists from list structure?

Hi I’m trying to sum the values in a series of sublists I can sum the values in the lists that aren’t empty just fine, but my problem is I also have sublists that are empty and the math.sum node get rid of those empty lists which I’d like to have returned as 0 or empty list thus retaining the list structure, if you see the image i have 289 lists total, but math.sum reduces it 221 because it removes all the empty lists can this be fixed can someone please help with this?

Apologies I’m not at a PC right now… does hitting the ‘keep list structure’ box next to list level help?

If not, perhaps replace empty prior to summing…

If not, maybe show some more of your graph or a test graph and we can try to find a work around :slight_smile:

Edit: The problem is empty lists at the end, if you don’t need the L4 you are fine, the math sum erroneously clips off the empty list at the end, but that doesn’t matter…

Edit: If you want to keep the empty list at L4, I don’t think there is a node solution which works if there’s an empty list at the end of your list (there was a similar chat on the forum this morning)…

Hope that helps,

Mark

@raymond.humes

Copy this code in the Python Script Node

OUT=[sum(lst) if lst else [] for lst in IN[0]]

2 Likes

Provided you work with flat sublists, the standard node should work as well.

Here is my python version…

lists = IN[0]
outlist = []

for l in lists:
  outlist.append(sum(l))

OUT = outlist
1 Like

Hi thanks for your response and sorry for the bother could you tell me if I’m doing this right I’m new to python and not sure if I pasted your code in the right way its currently giving me this error
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. Traceback (most recent call last): File “”, line 8, in
TypeError: unsupported operand type(s) for +: ‘float’ and ‘str’

Also I just found a new error in the work flow I’m trying to accomplish that can’t be solved by the Math.Sum node, but may or may not be solved by your code, when I get empty or null values it breaks the math.sum node and it returns the error warning asked to convert Non convertible types, so I can’t get the sum of the lists i’m looking for like 594.167 + 742.709 for example, can your code deal with this eventuality or can it be solved with dynamo nodes?

Hi Mark and thanks for the help I have a similar question to for you as I did for Salvatore and sorry I’m new to python and need a little further guidance I copied your code as follows, not sure if I did it right though I’m getting an error can you take a look

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 12, in
TypeError: unsupported operand type(s) for +: ‘float’ and ‘str’

And I also have the same question for you as I did for Salvatore and am not sure if your code solves another issue I have with what I’m trying to accomplish I just found a new error in the work flow I’m trying to accomplish that can’t be solved by the Math.Sum node, but may or may not be solved by your code, when I get empty or null values it breaks the math.sum node and it returns the error warning asked to convert Non convertible types, so I can’t get the sum of the lists i’m looking for like 594.167 + 742.709 for example, can your code deal with this eventuality or can it be solved with dynamo nodes?

change to this

def ConvertToNum(lst):
	out=[]
	for item in lst:
		out.append(float(item))
	return out

OUT=[sum(ConvertToNum(lst)) if lst else [] for lst in IN[0]]
1 Like

Hi sorry to keep bothering you with this, but I still can’t seem to get it working, right now I’m getting this following error can you help.

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 14, in
File “”, line 11, in ConvertToNum
ValueError: invalid literal for float():

Hey,

I’m also a complete beginner, much happier with nodes!

The data you are showing now is a bit different to the original…

I think the error here is misleading, what you have is a list with no values, the python has interpreted the value of this as a string for some reason. That was why @salvatoredragotta introduced a conversion to float (a kind of number) but Python can’t do that conversion because there is no string to convert.

I have had a play with trying to get the code to respond to your error and put a 0 in, but it’s not working. Hopefully someone else will beat me to it! Otherwise perhaps post it to StackOverflow? There seem to be lots of similar queries that people have answered…

Apologies,

Mark

Tried just adding a “try” clause which returns a 0 for all errors (which is all non-convertible types).

But would also work with an if statement seaching for empty strings…

Fantastic work, thanks! So @salvatoredragotta 's approach (and the error) was correct, the empty values are interpretted as strings how odd! Great to know they Python syntax for empty string :slight_smile: Thanks!

I have not read through everything (sorry) but would this work?

Edit: My apologies, it looks like @Mark.Ackerley has already done this.

1 Like

I think the gist is that the OP would like the list structure to remain thus keeping the “empty lists” in the summed result :slight_smile:

Mind that the python approach “breaks” if the top list contains single numbers (not in lists).

If this is to be done I would cover all my bases and add another layer of try clauses :slight_smile:

1 Like

Let me know how this works:

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
_list = IN[0]
_subSum = []

for _intList in _list:
	c = 0
	if not _intList:
		_subSum.append([])
		continue
	else:
		for i in _intList:
			c += i[0]
	_subSum.append(c)
		
OUT = _subSum

Note that this only works for your specific structure, which was values 3 levels deep, or the main list being @L4 but can be easily adjusted, just let me know.

Edit: Just realized your list has a total 5 levels, not sure what the complete structure is like. I would need to see a better picture of the data.

1 Like

Hi Kenny,

Great to see these solutions :slight_smile:

Part of the problem is that an empty value is being read as an empty string rather than a float…

Cheers,

Mark

image

Hi Kenny,

This is my version of your code… apologies… I’m so bad at this…

Mark
image

_list = IN[0]
_subSum = []

for _intList in _list:
	if not _intList:
		_subSum.append([])
		continue
	else:
		c = 0
		for i in _intList:
			try:
				c += i[0]
			except:
				"fail"
	_subSum.append(c)
		
OUT = _subSum
1 Like