Help me to understand looping


I’m not getting why these two scripts give two different results?




In the first one collection is a list and in the for loop you are iterating over the numbers that exist in collection.

Whereas in the bottom one, x is itself a list which you put into another list. In other words you are making a list of list.

1 Like


To elaborate on what @HossZamani said:
The for loops, to me at least, are a little bit strange with how they handle their input.

If you say for (i in k), with k for example being 4, it’ll essentially go “if k contains i, do the thing”, meaning that it’ll increment i until i == k, but it does iterate and as a result you’ll get nulls for the intermediate values, i.e. [null,null,null,null,4].
Then, if you tell it to do the same but for (i in 0..k), it’ll work neatly.
Sort of.
Saying for (i in 2..k) will result in two leading nulls before working at 2,3,…,k.

In my opinion, for-loops don’t behave very nicely in this regard. They’re a bit more difficult to predict and they don’t always handle their input as you’d expect.

An alternative solution is using the following:
k=4; i=2; while (i <= k) {do_a_thing(); i = i+1;}

It gives you a lot more control over the way it iterates, including non-integer stepsize, with the trade-off that you really have to manually control it:. If you forget to add the i = i+n;-bit, it’ll loop forever, forcing you to force-close Dynamo and potentially lose work…

Which approach is best ultimately depends on what you’re looking to accomplish, but to me with a background in C, the DS for-loops are a nightmare to deal with :slight_smile:



I think you got his wrong. The CodeBlock will search for the values in k but will only find the value 4. Thus, i will only take the value 4. The null values are here because of output[i] = i; : you did not specify the values for output[0],output[1],output[2] and output[3] so null values are stocked there.


Regarding this and what I said earlier, you could totally do something like :


(so with no null values at the beginning of your list).



Ah, right, that makes a lot more sense :sweat_smile:
I was admittedly wondering how it was determining what to do with the lower values but of course the list would start at [0] regardless of what part we’re interested in.

You’re right, I hadn’t considered you’d then also need to offset the index of the output… The more you know!
Still not a fan of the for-loop syntax, but I admit it’s a bit more sensible than I gave it credit for.

1 Like


Although @HossZamani already answered the question, the loop in your graph isn’t necessary, as you can simply do the following :

x = [0..10..#5];
point = Point.ByCoordinates(x);

and get the result you want. But your problem is more of a general problem and this is but an example probably…

1 Like


Thanks @mellouze for elaborating this. now it is making more sense to me.
Yeah i made this for not any actual purpose but as an example.
thanks for all the helps.
Thanks @HossZamani and @Avz.