"If"-statement providing the wrong output

Hi everyone.

My first post here. I’m still very new to Dynamo, but I’ve gotten a few things working.
Right now I’m a bit puzzled by a simple if-statement.

The statement is:
“If there are sublists then flatten the input-list, and if there’s not, then give me the input-list without flattening”.

inVal == false ? List.Flatten(inlist,amt) : inlist;

However this always gives me the “inlist” without flattening. Also when the “inVal” is “false”.

If I alter the code to:

inVal == false ? List.Flatten(inlist,amt) : 1;

Then it gives me the correct result by giving me the value “1” whenever “inVal” is “true”, and the flattened list when “inVal” is “false”.

Can anyone explain this, or see where I’m doing the wrong thing?
Any suggestions to a better way of doing the test is also very welcome.

Thank you all in advance.

Image of sequence that works:

Image of sequence that won’t work:

Thomas,

Does this get you what you want. It is a little long winded I am afraid.

Hei Thomas!

I think a flatten and list map will do what you want:

1 Like

Hello @Thomas_Pedersen1,

Here you go with another example.

Do let me know if that doesn’t work with your example.

Happy to help.

Thanks,
Ritesh

1 Like

Just as Reinar says: I don’t think you need more than a list map and flatten:

Thank you everyone for all your input.

I thought I needed a function to check if there was more than one layer of sublists and in that case, flatten the list down to one layer, and if there was only one layer then just output the original list.

The thing that puzzled me, was the fact that it seemed to me, that the if-statement was giving me the wrong output (the unflattened list) no matter if the if-statement was true or false.

I still haven’t figured that one out yet, but the input from you guys still really helped me to the right path.
The simple solution with a List.Map and a Flatten node was the thing I needed in my case. So much more simple than I thought it ever could be done.
However I think I need to dig a little deeper into the functionality of the flatten node, as I’m not entirely sure why it works for me here, but it’s perfect that it does.

Thank you all again.

Here’s the final solution with more than one layer of sublists:

And the result with just one layer of sublist:

Oh and thank you for showing me the Rank-node Ritesh_Chandawar :slight_smile:
I didn’t use it here, but it did the thing I was trying to achieve with my inlist[0][0][0], so it’s great to know it for another time.