# Dynamo Python Script Error

**URL:** <https://forum.dynamobim.com/t/dynamo-python-script-error/73941>\
**Category:** Revit\
**Tags:** revit, python, dynamo\
**Created:** [March 3, 2022, 10:26pm UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941 "2022-03-03T22:26:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jguevara5PPA8](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jguevara5PPA8](https://forum.dynamobim.com/u/jguevara5PPA8)\
**Post date:** [March 3, 2022, 10:26pm UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/1 "2022-03-03T22:26:45Z")

</div>

Hi everyone I have some problems trying to get the result with this dynamo python script all the inputs that I bring to the code seems to be fine, but when try to iterate all of them with an if statements prompts me an error “object cannot be interpreted as an index”. I am still learning with python and I do not know what should be the problem. If anyone knows I will really appreciate regards.

```auto
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
 
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
	
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
pol = []
#Geeting Voltage Drop by Num_Poles

for p in Poles:	
	if p == 1:
		pol = 2 * (current * res * (len/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
		result.append(pol)
	elif p == 3:
		pol = 1.732 * (current * res * (len/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
		result.append(pol)
OUT = result

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/e/ae6e23f76a0ee7a66915653dc0aa31d586cca8fe.jpeg)

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [March 4, 2022, 12:07am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/2 "2022-03-04T00:07:59Z")

</div>

I believe the code is OK, so it might be your inputs. Make sure they are all of a numerical type (integer/double) and not in a list format. If you want to work with lists of values and poles in parallel you should use the zip() iteration method which looks a bit like this…

```auto
example1 = [1,2,3]
example2 = [10,20,30]

results = []

for e1,e2 in zip(example1,example2):
	result = e1 + e2
	results.append(result)

print(results)

would show...

[11,22,33]

```

---

<div class="post-metadata">

**Author:** ![SeanP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seanp/32/42560_2.png) [@SeanP](https://forum.dynamobim.com/u/SeanP)\
**Post date:** [March 4, 2022, 12:38am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/3 "2022-03-04T00:38:04Z")

</div>

Do not specify pol as a list with your other variables because your not using it as such.

Basically what your doing is trying to tell the list pol to actually be a double which doesn’t work.

---

<div class="post-metadata">

**Author:** ![Cezmi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cezmi/32/96065_2.png) [@Cezmi](https://forum.dynamobim.com/u/Cezmi)\
**Post date:** [March 4, 2022, 1:11am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/4 "2022-03-04T01:11:42Z")

</div>

@jguevara5PPA8

I think you need to add either “len” as a variable or you need to call it “leng”?

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/7/276817a798af20240fabbf86db0089f1601117a9.png)

```auto
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
 
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
	
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
pol = []
#Geeting Voltage Drop by Num_Poles

for p in Poles:	
	if p == 1:
		pol = 2 * (current * res * (leng/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
		result.append(pol)
	elif p == 3:
		pol = 1.732 * (current * res * (leng/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
		result.append(pol)
OUT = result
type or paste code here

```

Otherwise your code is working.

---

<div class="post-metadata">

**Author:** ![jguevara5PPA8](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jguevara5PPA8](https://forum.dynamobim.com/u/jguevara5PPA8)\
**Post date:** [March 4, 2022, 1:58am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/5 "2022-03-04T01:58:14Z")

</div>

Thank you everyone for your comments this what I got so far, I believe with the for in zip function gives me more results that I need. for instance all the input list got 187 items which is all the values that i got from the project. with my new code instead of that items that i need for the list i got 34969 items, seems to me is like kind of cross product with a node in Dynamo correct me if I wrong regards.

```auto
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
 
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
	
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
#Geeting Voltage Drop by Num_Poles
for p in Poles:
    if p == 1:
        for curr, res1, leng1, power1, reac1 in zip(current, res,leng, power,reac):
            x = 2 * (curr * res1 * (leng1/1000 ) * power1) + (curr * reac1 * (leng1/1000 ) * math.sin(math.acos(power1)))
            result.append(x)
    elif p == 3:
        for curr, res1, leng1, power1, reac1 in zip(current, res,leng, power,reac):
            x = 1.732 * (curr * res1 * (leng1/1000 ) * power1) + (curr * reac1 * (leng1/1000 ) * math.sin(math.acos(power1)))
            result.append(x)
    else:
        result.append("No voltage Drop")
OUT = result

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/0/f0bb10fd41437bed04f3b942bbb722bd2b76703e.jpeg)

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [March 4, 2022, 3:08am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/6 "2022-03-04T03:08:29Z")

</div>

Again, we need to know your list structure(s). If they are all parallel to p, zip at the top level of the for loop, not the nested one.

---

<div class="post-metadata">

**Author:** ![Cezmi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cezmi/32/96065_2.png) [@Cezmi](https://forum.dynamobim.com/u/Cezmi)\
**Post date:** [March 4, 2022, 3:12am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/7 "2022-03-04T03:12:59Z")

</div>

I usually use “cycle” function in my Python code to rotate for each item in my lists. So you can control the the list length.  
I don’t have other values that feeds your code but I tried to mimic your code.  
here is the first version which is the longest:

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/e/ae700c901bb4b3973675b6137c1351f40eb1e3ee.png)

and the code you need to use below:

```auto
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
 
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
from itertools import cycle

data = IN[0]
list_one = IN[1]
list_two = IN[2]

output = []

for i in data:

    if i == 1:
        for lst1, lst2 in zip(list_one, cycle(list_two)):
            total = lst1 + lst2
            output.append(total)

    elif i == 2:
        for lst1, lst2 in zip(list_one, cycle(list_two)):
            total = lst1 / lst2
            output.append(total)

    elif i == 3:
        for lst1, lst2 in zip(list_one, cycle(list_two)):
            total = lst1 * lst2
            output.append(total)

    else:
        output.append("No Voltage")

OUT = output

```

here is the second version which is the shortest:

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/e/fe069c6c30b77c5e444035f60c09e9bab993b5e4.png)

and the code you need to use below:

```auto
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
 
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
from itertools import cycle

data = IN[0]
list_one = IN[1]
list_two = IN[2]

output = []

for i in data:

    if i == 1:
        for lst1, lst2 in zip(cycle(list_one), list_two):
            total = lst1 + lst2
            output.append(total)

    elif i == 2:
        for lst1, lst2 in zip(cycle(list_one), list_two):
            total = lst1 / lst2
            output.append(total)

    elif i == 3:
        for lst1, lst2 in zip(cycle(list_one), list_two):
            total = lst1 * lst2
            output.append(total)

    else:
        output.append("No Voltage")

OUT = output

```

Hope that helps…

I also agree with @GavinNicholls

---

<div class="post-metadata">

**Author:** ![jguevara5PPA8](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jguevara5PPA8](https://forum.dynamobim.com/u/jguevara5PPA8)\
**Post date:** [March 4, 2022, 3:27am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/8 "2022-03-04T03:27:55Z")

</div>

Yes all the inputs has the same items, so probably that the reason why I got the cross product. See the image down below thank you for the help everyone.

 ![voltage drop in Dynamo REV.B_2022-03-03_11-47-00](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/c/cc870cf7fd437e6e78c78756db80fc2edeb59d07.png)

---

<div class="post-metadata">

**Author:** ![Hyunu\_Kim](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/hyunu_kim/32/100239_2.png) [@Hyunu\_Kim](https://forum.dynamobim.com/u/Hyunu_Kim)\
**Post date:** [March 4, 2022, 5:22am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/9 "2022-03-04T05:22:48Z")

</div>

I think you should explain more about your graph and What’s the result for.  
And it may easier to understand with less items, like 3 or 4 items…not your real hundreds items.

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [March 4, 2022, 7:44am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/10 "2022-03-04T07:44:02Z")

</div>

> [@jguevara5PPA8](#):
>
> ```auto
> for p in Poles:
> if p == 1:
> for curr, res1, leng1, power1, reac1 in zip(current, res,leng, power,reac):
> 
> ```

Hello,

the code should look like this instead

```auto
for curr, res1, leng1, power1, reac1, p in zip(current, res,leng, power,reac, Poles):
	if p == 1:
		# rest of code for voltage drop "single phase" calculation
	elif p == 3:
		# rest of code for voltage drop "three phase" calculation
	else:
		# rest of code

```

---

<div class="post-metadata">

**Author:** ![jguevara5PPA8](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jguevara5PPA8](https://forum.dynamobim.com/u/jguevara5PPA8)\
**Post date:** [March 7, 2022, 12:22am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/11 "2022-03-07T00:22:36Z")

</div>

Hi every one after doing a a lot of changes with the script, I finally got what I want. Here the final script thank you for the help.

```auto
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
 
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
	
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
#Geeting Voltage Drop by Num_Poles
if p == 1:
    for curr, res1, leng1, power1, reac1, p in zip(current, res,leng, power,reac, Poles):
        x = 2 * (curr * res1 * (leng1/1000 ) * power1) + (curr * reac1 * (leng1/1000 ) * math.sin(math.acos(power1)))
        result.append(x)
elif p == 3:
    for curr, res1, leng1, power1, reac1, p in zip(current, res,leng, power,reac, Poles):
        x = 1.732 * (curr * res1 * (leng1/1000 ) * power1) + (curr * reac1 * (leng1/1000 ) * math.sin(math.acos(power1)))
        result.append(x)
    else:
        result.append("No voltage Drop")
OUT = result

```

---

<div class="post-metadata">

**Author:** ![SeanP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seanp/32/42560_2.png) [@SeanP](https://forum.dynamobim.com/u/SeanP)\
**Post date:** [March 7, 2022, 3:59am UTC](https://forum.dynamobim.com/t/dynamo-python-script-error/73941/12 "2022-03-07T03:59:52Z")

</div>

Although I understand this code may be your resolution, it’s typically good practice to mark the post which provided the path or suggested the approach that resolved your issue as the Answer. This gives credit to those that help you find the solution.
