How Can I Create a Loop in Dynamo?

Hello,
I’m importing revit page numbers from an excel file. Then I translate these into page names. Afterwards, I automatically go to the views they are in and do the PDF and DWG conversion. However, it only does this for the cell in the 1st row of Excel. I can choose which row to convert by adding a parameter, but how can I do it all at once?

Or, can I loop it with parameters?

image

Can you show thr data with a watch node after the last string replace

If you remove the List.GetItemAtIndex node and reconnecting it should process the full list

Unfortunately I tried it and I get this error.

image

Can you post the full graph using ‘Export as Image’ or upload the dyn file?

Half of those are blank - if you zoom in you can’t see any text.

If you want to loop in Dynamo then use the ‘count’ node.

So if you have 10 items use the count node to tell you it’s 10 in that list. then type
0… n-1 in a codeblock. Plug the count node into the codeblock, that’s your n value.

That’ll create a range. You start at zero because dynamo is zero indexed and n-1 will give you 9 in this instance which is the last item in your list.

Use this range to tell Dynamo which excel rows you want to read.

Loops are a lot easier using a Python block.

Is it possible to share a sample code?

So you’ve got the count.

Now feed that into a code block where you’ve typed: 0…n-1
then use that instead of your slider.

You should then get a list.

Can you share an example code block?

Do you know what a code block is?

Double click on the Dynamo canvas. A code block will appear.

Click inside the block and type: 0…n-1

That is all you need to do.

Feed the count number into the code block afterwards.

I didn’t do it as a code block, but I wrote such a code in Python.

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *



msg = IN[0]


try:
    finish = int(msg)
except ValueError:
    finish = 0

# İlk sayı
first = 0


number_list = []


for i in range(first, finish):
    number_list.append(i)


OUT = number_list

I had a look in the primer but can’t actually see much about code blocks (there are some pictures of them in use though)
https://primer.dynamobim.org/04_The-Building-Blocks-of-Programs/4_the-building-blocks-of-programs.html

Code blocks are great, you can use them for all sorts of things, here are a few examples.

Seems quite long winded to me, I’d write that like this:

x = 0
OUT = []

for i in range(IN[0]):
	OUT.append(x)
	x += 1

The problem is not there, it does not match the resulting values ​​when we send them as an array. However, it works when I send it individually.

I have no idea what your data looks like - maybe show some of the outputs of the nodes.

But I’m going to suggest that you try changing the lacing of the String.Replace nodes…

Also, if you can write Python it would be easier doing the lot of that in Python.

When you enter 1 or a different number in the index value, it works. However, when I send it as a list, it doesn’t work.

Eureka

You need to look into list levels. That is how you control “looping” in Dynamo nodes. Dynamo automatically iterates over an input structure (i.e. loops) but you sometimes have to specify how that list structure should be handled. That’s where list levels come in.

The primer should cover this but there are also plenty of topics here on the forum that go into detail over this exact issue as well.