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

x[0][8]
x[1][8]
x[2][8]
x[3][8]
x[4][8]

(iterating though x[n][8])

All help appreciated. Thanks.

dynamo_addtoList

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:

List
0 1234-PROJECT-CLIENT-ARQ-S01-EXE-P01B0-BIM-0001-PLANTA-S01-1
1 1234-PROJECT-CLIENT-ARQ-S01-EXE-P01B0-BIM-0002-CORTE-S01-1

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.

Thanks.

Hi @bmoreira Probably something here could work…

3 Likes

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

solution

1 Like

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

2 Likes