If statements and lists

Hi guys,

Trying to make a if statement. I don’t fully understand why some things are ‘result’

Made a simple example:


The green groups are what I’ve expected when I use IF

When I start using lists, for true or false, I aways get a list with 1 or more items. It doesnt matter if the statement is true or false. See the pink groups.

In the ‘true with list’ example i’d expect to get ‘5’ as result. Not 5 and 5 in a list.

Why is this happening and how to solve to get my expected result?

@WTH_MGA,

How interesting! Not sure exactly why True with List returns a list like that. However I imagine it might be useful somewhere… :thinking:

Anyway, if this is causing you some issues. You can use an alternative node called Scopeif. Which does not duplicate your “5”.

See below:

Been some discussion on IF functions here. Some interesting alternatives to IF node.

Best,

Ben

1 Like

use scopeif instead

Thanks. The result is what I expected!

But when I apply it to my real script dynamo crashes!

When I open the file and press run, it rus ok. When I change True to False, dynamo crashes.

ScopeIf is mainly used for custom nodes. It actually prevents the unused output from running so it only works in completely linear graphs. The problem (if you hadn’t noticed) is mismatched list lengths.

A codeblock is generally the best way to go:

i = test?0:1;
{true_opt,false_opt}[i];

This uses indices to pull the correct output which allows you to ignore any list structure issues.

5 Likes

The above code is Dynamo 1.X formatted. 2.x is:

i = test?0:1;
[true_opt,false_opt][i];

Alternatively you can use a single line as follows:

[trueOption, falseOption][test?0:1];

6 Likes

It is working now thank you all!