For Loop Python - Index Out of Range

Starting to dig into python now, as opposed to sticking to just prebuilt nodes in dynamo.

I am trying to utilize the ToXyz revit api function in bigger python script and have pinpointed the problem to a specific part of my code.

Please see my python script below and dynamo input:


When I run this, i get that an error that states index 0 is out of range. This is confusing, as my input values definitely has a value at index 0, so I am not sure why the code is stuck.

Any help is appreciated, I am able to get this python script to work without looping.

Thanks!

@austin.guter try:

for point in startPs:
 lineloads.append(point.ToXyz)

Thanks Sal!

in an effort to teach me to fish rather than give me a fish, can you please explain why your method works and mine does not?

I appreciate your help!

@austin.guter search on google on how to use For loops & append. You are just using the wrong syntax.

@austin.guter Don’t forget to mark the post as solved

1 Like

Thanks Sal, a few questions that I think is inherently different in the python than from what I am used to with my programming background (I’ve only used matlab before for coding).

How does python know how long to iterate a “for” loop for in your example above? All that is mentioned is a variable? Does python somehow know the length of the variable?

In the output (lineloads in the example) we give no indices to the solution, how does the answer know where to be saved in the loop without an index? I am guessing this has something to do with the append function?

also interesting, it appears as though the looping value (point in the example) pulls the actual value in a list, without the need to reference an index.

I am going to do a little more general research on python, as this still feels quite foreign to me at this point.

@austin.guter Maybe this link clarifies

https://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/