Python for Dynamo; error for loop: index out of range

Hi, I am kind of a noob of Python, but I am using Python to build a node in Dynamo for Civil 3D, and I’m facing some problems with the following loop, giving me the error “Traceback (most recent call last): File “”, line 16, in IndexError: index out of range: 0”

Strategy = IN[0]
TotStrategies = IN[1]
Packs_C_D = IN[2]
Packs_B = IN[3]
Packs_A = IN[4]
Packs_Indexes=[]
i=0
for i in range(0,TotStrategies,1):
    if Strategy[i] == 'a':
        Packs_Indexes[i]=Packs_A
    elif Strategy[i] == 'b':
        Packs_Indexes[i]=Packs_B
    else :
        Packs_Indexes[i]=Packs_C_D
OUT = Packs_Indexes

The script should build a new list (Packs_Indexes) putting the elements (lists themselves: Packs_A, Packs_B, Packs_C_D) at the index i consequently to a condition.

I also tried with

for i in range(0,TotStrategies,1):
    if Strategy[i] == 'a':
        Packs_Indexes.append(Packs_A)
    elif Strategy[i] == 'b':
        Packs_Indexes.append(Packs_B)
    else :
        Packs_Indexes.append(Packs_C_D)
OUT = Packs_Indexes

In this case, I don’t have the same problem, but the calculation goes on for hours and then eventually the software crash.

What am I doing wrong?

Thanks to whoever will help me out! :smiley:

Is Strategy a list?

Yes.

Strategy is a list of 1 dimension, as well as Packs_A and Packs_B while Packs_C_D is a lists of 2 dimensions.

trying using it like this

for i in range(0, len(TotStrategies)):

1 Like

done and sadly it doesn’t help… I also tried with a “while” loop, or specifying the step in the for loop. I have the idea that maybe the problem is in the deepness of the lists (vectors and matrix, basically), but it shold work nicely as it did in other script I used before

Can you provide the list structures / images of the graph with node preview so we can better understand what the data looks like?

Oh, I’m sorry, I misunderstood, I thought you meant len(Strategy), that is the vector of strategies, while TotStrategies is an int. I’ll post the pics of the graph as you asked

Pack_Indexes is empty, therefore the call to Pack_Indexes[i] will fail.
You need to use Pack_Indexes.append() instead

2 Likes

OK, starting problem solved… that’s basically why when I use “append” it doesn’t give me any error, but on the other hand the software crashes after few minutes it’s running. I hope it is not an hardware problem (and anyway I also tried to use a virtual memory to bust the ram).

Do you read and write to the same Excel file?

check, yes

so it’s an infinite loop, don’t do that.

2 Likes

Thank you, but still I dont get it: how can it be an infinite loop if the range has a beginning (0), an end (TotStrategies (=54)) and a step (1)? I mean, the condition to start the loop should stop to be true when i=55! :frowning:

The Dynamo file is an infinite loop:

  1. Has the Excel file Changed? Yes, Read the Content
  2. Process the data
  3. Write To Excel > This triggers (1)
2 Likes

Again, thank you so much for your kind reply;
I wasn’t clear before: the Dynamo file doesn’t write on any excel file (I didn’t arrive at that point yet, and I’ll write on a different excel file).

Eventually, it was a hardware problem. The script was correct, but the amount of calculations was too much for my 8 gb RAM, it needed a 64gbRam workstation to run successfully…

Thanks to everybody for your replyes!