# How Can I Create a Loop in Dynamo?

**URL:** <https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689>\
**Category:** Revit\
**Tags:** dynamo\
**Created:** [September 12, 2023, 6:56am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689 "2023-09-12T06:56:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 6:56am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/1 "2023-09-12T06:56:22Z")

</div>

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](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/0/f0b37ae6a48d45708458a637ab5d5533296b2eaa.png)

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

---

<div class="post-metadata">

**Author:** ![pyXam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/pyxam/32/163009_2.png) [@pyXam](https://forum.dynamobim.com/u/pyXam)\
**Post date:** [September 12, 2023, 7:00am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/2 "2023-09-12T07:00:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [September 12, 2023, 7:48am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/3 "2023-09-12T07:48:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 7:56am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/4 "2023-09-12T07:56:16Z")

</div>

Unfortunately I tried it and I get this error.

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

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [September 12, 2023, 8:01am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/5 "2023-09-12T08:01:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 8:03am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/6 "2023-09-12T08:03:50Z")

</div>

![SG_Revit_Excel_PDF_DWG_2023-09-12_11-03-14](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/2/f248a6c9184afb487b6c736c04d3ea40d824a655.png)

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 12, 2023, 8:11am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/7 "2023-09-12T08:11:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 9:01am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/8 "2023-09-12T09:01:45Z")

</div>

Is it possible to share a sample code?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/5/05fe7ef08bcca87535265f7f97ff928a066ff42e.png)

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 12, 2023, 9:03am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/9 "2023-09-12T09:03:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 10:03am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/10 "2023-09-12T10:03:20Z")

</div>

Can you share an example code block?

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 12, 2023, 10:05am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/11 "2023-09-12T10:05:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 10:13am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/12 "2023-09-12T10:13:28Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/4/74a07fcde99c77ed8e848fb93edc1f810b5850fb.png)

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

```auto
# 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

```

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 12, 2023, 10:14am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/13 "2023-09-12T10:14:48Z")

</div>

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](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.

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

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 12, 2023, 10:20am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/14 "2023-09-12T10:20:57Z")

</div>

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

```auto
x = 0
OUT = []

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

```

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 10:22am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/15 "2023-09-12T10:22:49Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 12, 2023, 10:25am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/16 "2023-09-12T10:25:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 10:31am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/17 "2023-09-12T10:31:57Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/0/50d4660ca1a4ecf7a013ea68abb7ab1cfa2c2d15.png)

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 10:37am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/18 "2023-09-12T10:37:19Z")

</div>

## Eureka

---

<div class="post-metadata">

**Author:** ![sefa](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sefa](https://forum.dynamobim.com/u/sefa)\
**Post date:** [September 12, 2023, 10:37am UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/19 "2023-09-12T10:37:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 12, 2023, 1:49pm UTC](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689/20 "2023-09-12T13:49:33Z")

</div>

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.

[Next page](https://forum.dynamobim.com/t/how-can-i-create-a-loop-in-dynamo/93689.md?page=2)
