For/while loop to produce a single list by iterating through

I am trying to append a specific number of items to an existing list. In the example in the image below the list “a” has one item (0001) and then I append 2 more items (0002 and 0003).

The number of items to append is known but can vary, so I am looking to a way (for / while loop?) that can append that variable number of items and produce one single list.

For example, if I have 5 items, I need to create a list that would iterate through the values of 0-4 and produce the list:

0001 [n=0]
0002 [n=1]
0003 [n=2]
0004 [n=3]
0005 [n=4]

which would be the result of evaluating


(iterating though x[n][8])

All help appreciated. Thanks.


First off, since Dynamo automatically iterates through lists, loops are rarely needed and instead just handled through list management and properly defining your function.

I’m not exactly sure where your specific logic is coming from but you have some way better options available. If you know the starting number of items and the number you need to append, then you already know how many times run the function. Meaning, you can just create the necessary items and then append them. In your case, it seems like you already have all the items (since you’re appending them and not creating them) which means you should be able to use List.TakeItems to get all the additional items you need based on the known number you’re appending.

If you could show us the data you already have that would be helpful. Using your specific list structures without knowing what those lists look like is a bit confusing.

Hi @bmoreira,

First of all: Welcome to the Dynamo community! :slight_smile:

Do you want/ mean a solution like this?

2021-11-05 Add leading zeroes to counted list.dyn (10.4 KB)

Thank you @Nick_Boyts and @Daan for reaching out! I’ll be more precise, and maybe you are right and things can be more simple than I am thinking. Here’s an image of what I am trying to achieve:

I have the following list:

and I would like to transform it to:


and so on. The script would need to check how many items there are to process (in this case there are 3) and concatenate the elements, separated by “-”, producing a new list as a result.


Hi @bmoreira Probably something here could work…


Thanks! I was just trying something similar. Thanks for pointing me in the right direction!


1 Like

Great to see you found a solution!
Most times the solution is easier then you would think :slight_smile: