# Add corresponding values of two equal length lists

**URL:** <https://forum.dynamobim.com/t/add-corresponding-values-of-two-equal-length-lists/2006>\
**Category:** Packages\
**Created:** [August 16, 2015, 10:51am UTC](https://forum.dynamobim.com/t/add-corresponding-values-of-two-equal-length-lists/2006 "2015-08-16T10:51:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Rowland](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Steve\_Rowland](https://forum.dynamobim.com/u/Steve_Rowland)\
**Post date:** [August 16, 2015, 10:51am UTC](https://forum.dynamobim.com/t/add-corresponding-values-of-two-equal-length-lists/2006/1 "2015-08-16T10:51:20Z")

</div>

Hej,

I have two lists of doubles of equal length. I need to sum the corresponding, and write the values to a new list. It seems like a simple task, but I am getting tripped up.

list a = [1.0 , 2.0 , 3.0 , 4.0 , 5.0]

list b = [2.0 , 5.0 , 4.0 , 2.0 , 1.0]

add the corresponding value to list c. So what I want is:

list c = [3.0 , 7.0 , 7.0 , 6.0 , 6.0]

Unfortunately I am getting:

list c = [1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 2.0 , 5.0 , 4.0 , 2.0 , 1.0]

&nbsp;

I’ve written a short Python script to do the job, but I have a semantic error (it works but not as intended). Anybody have a good idea? Here’s the code:

\<span style=“font-weight: bold; color: #f92672;”\>import\</span\> clr  
clr.\<span style=“font-weight: bold; color: #ffffff;”\>AddReference\</span\>(\<span style=“color: #e6db74;”\>‘ProtoGeometry’\</span\>)  
\<span style=“font-weight: bold; color: #f92672;”\>from\</span\> Autodesk.DesignScript.Geometry \<span style=“font-weight: bold; color: #f92672;”\>import\</span\> \*  
\<span style=“color: #75715e;”\>#The inputs to this node will be stored as a list in the IN variables.\</span\>  
dataEnteringNode = IN[\<span style=“color: #ae81ff;”\>0\</span\>]  
dataEnteringNode = IN[\<span style=“color: #ae81ff;”\>1\</span\>]

\<span style=“color: #75715e;”\>#Declare variables\</span\>  
a = IN[\<span style=“color: #ae81ff;”\>0\</span\>]  
b = IN[\<span style=“color: #ae81ff;”\>1\</span\>]

\<span style=“color: #75715e;”\>#Add the respective values of each list\</span\>  
\<span style=“color: #75715e;”\>#c= [x+y for x,y in zip(a, b)]\</span\>  
\<span style=“color: #75715e;”\>#c = [a[i] + b[i] for i in range(len(a))]\</span\>  
\<span style=“color: #75715e;”\>#c = map(lambda x,y:x+y, a, b)\</span\>

\<span style=“color: #75715e;”\>#Assign your output to the OUT variable.\</span\>  
OUT = c

&nbsp;

As you can see, I have 3 attempts to make it work right ( I have commented them out right now). Perhaps I am missing something obvious.

&nbsp;

---

<div class="post-metadata">

**Author:** ![Steve\_Rowland](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Steve\_Rowland](https://forum.dynamobim.com/u/Steve_Rowland)\
**Post date:** [August 16, 2015, 10:58am UTC](https://forum.dynamobim.com/t/add-corresponding-values-of-two-equal-length-lists/2006/2 "2015-08-16T10:58:41Z")

</div>

sorry about all the color style nonsense. Here’s the code again as something more readable.  
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.  
dataEnteringNode = IN[0]  
dataEnteringNode = IN[1]

#Declare variables  
a = IN[0]  
b = IN[1]

#Add the respective values of each list  
#c= [x+y for x,y in zip(a, b)]  
#c = [a[i] + b[i] for i in range(len(a))]  
#c = map(lambda x,y:x+y, a, b)  
#Assign your output to the OUT variable.  
OUT = c

You can see my 3 attempts (that don’t work)!

---

<div class="post-metadata">

**Author:** ![Steve\_Rowland](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Steve\_Rowland](https://forum.dynamobim.com/u/Steve_Rowland)\
**Post date:** [August 16, 2015, 11:42am UTC](https://forum.dynamobim.com/t/add-corresponding-values-of-two-equal-length-lists/2006/3 "2015-08-16T11:42:55Z")

</div>

Okay fixed my own problem! (hooray) It was a dynamo issue rather than a python issue.

&nbsp;

![Element-wise](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/2X/c/c1876f3bd73903cc35ab21b69c425e25fdb674db.png)

---

<div class="post-metadata">

**Author:** ![Konrad\_K\_Sobon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/konrad_k_sobon/32/8838_2.png) [@Konrad\_K\_Sobon](https://forum.dynamobim.com/u/Konrad_K_Sobon)\
**Post date:** [August 16, 2015, 12:15pm UTC](https://forum.dynamobim.com/t/add-corresponding-values-of-two-equal-length-lists/2006/4 "2015-08-16T12:15:52Z")

</div>

It’s indeed very simple and requires no Python:

![Capture](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/2X/2/2282936c7872920045ec6ed7492100738a4e5892.png)
