Code Block: Filter by conditional statement, eradicate nulls


I am trying to learn Code Block syntax, and currently toying with creating my own List.FilterByBoolMask with a simple statement; “If Input==10?”, Else; Output==null. I would love to filter the output from the alternative value, and just output the In list. Is this possible within the same Code Block?

Hi @Tue_Kappel - You can keep chaining conditions like so - Is this what you are after?

1 Like

You can also call the dictionary outputs of nodes like so:

2 Likes

And you can replace Nulls like so :slight_smile:

1 Like

Something like this also may work

1 Like

To expand on what @solamour indicated around pulling values form a dictionary:

To call the Dynamo function and only return the ‘in’ dataset:
List.FilterByBoolMask(list,filter)['in'];

To be clear, the reason you need to call for the value from a dictionary via key (the ['in'] in the above statement) is that any node with multiple outputs in Dynamo is actually returning a dictionary, where the keys are the output names.

Another way to do this via your own function is as follows. You need to leverage imperative code, a floor loop, and an if statement in this, so it isn’t for the faint of heart. However for those same reasons it’s a good learning exercise. Towards that end I’ve included a comment before each line to outline why I wrote it.

//define the function name, and inputs with structure
def FilterList (list: var[], boolMask: bool[]) 
//start the definition block
{
	//start the return statement as an imperative code block 
	return [Imperative]
	//start the imperative code block
	{
		//define a new list for results
		results = [];
		//define a for loop to iterate over th indicies of the list
		for (i in 0..List.Count(list) )
		//start the for loop code block
		{
			//get the mask at the index
			msk = boolMask[i];
			//define an if statement for the mask
			if (msk)
			//start the if statement block
			{
				//redefine results to include the item from the list
				results = List.Join([results, list[i]]);
			//close the if statement block
			}
		//close the for loop
		}
		//define the output from the imperative block
		return results;
	//close the imperative block
	}
//close the definition
};

The results in action:

2 Likes

thank you all for great suggestions. I will try them all, and try to understand. -None of them what i was dreaming of, but all the more teaching moments for this simple mind.

3 Likes

I found the solution i was looking for…..-List.Clean can remove the null values, and throw away indices via the false value. So i’m left with the list of elements testing positive.Succes!

2 Likes

One liner for the pythonistas

Alternative

3 Likes

Nice one :wink: and another maybe, but not so cool as yours :wink:

hahah see now its the same as your alternative :wink: forget it :wink: really love the first one…

4 Likes

hi

edit: nice types in output on codeblock, I like it a lot.:ok_hand:

cordially

christian.stan

1 Like