Iterating through list to calculate the summation of the list

Hi everyone,

I am trying to iterate through the a list, and this list can have any number of items. In my example, as you can see below i tried to iterate through the list with no luck and i am getting the error of
"
IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 15, in
TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’"

the code I used to iterate inside pythonscript is:

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.

l= IN

s=l

sum1 = 0
a=
for x in s:
sum1 =sum1 + x

a=sum1

Assign your output to the OUT variable.

OUT = a

Any ideas on how to solve this ?

Many thanks in advance

Hi @saeedy3,

The input isn’t correctly defined. You must write IN[0] and not IN :

l = IN[0]

sum1 = 0

for x in l:
	sum1 =sum1 + x

OUT=sum1

sum

1 Like

I missed my lunch, no energy to idenitfy the mistake :sweat_smile:

Many thanks @Alban_de_Chasteigner you are a legend :slight_smile:

1 Like

@saeedy3 or you could also use the builtin function sum() :slight_smile:

image

1 Like

Brilliant answer. Thank you @salvatoredragotta

1 Like