List Levels in IF Statement | Code Block

Hey everyone!

I’m trying to get the First Item of the List @L2 in a IF Statement in a Code Block.
When i try it without the IF Statement it works perfectly fine.

Can someone please help me?

Hi @massimoprinoth can’t you just split the IF-statement and the First Item? So that you always create the full list or the first item and then take the correct one afterwards.

These imperative code blocks are such a pita. So much more straightforward in python.

Input = IN

if (Input != None):
	myResult = (Input[0])[0]

This will give you [1,2]

Input = IN

if (Input != None):
	myResult = ((Input[0])[0])[0]

This will give you 1.

Input = IN

if (Input != None):
	myResult = ((Input[0])[0])[1]

This will give you 2.
Of course the index could be replaced with variables to pull any item from any level.
And note: use -1 for last item.

The one tricky thing with Python is you need to handle list levels yourself :slight_smile: Doable of course, but a little bit of a learning curve for the beginner! (Or, you can wrap it in a Custom Node to leverage all that awesomeness and not have to worry about it so much).

For doing this in DesignScript, here is an option, courtesy of @Aparajit_Pratap ! :raised_hands:

And to have it in a single Code Block you can do as follows:

This is mixing both Associative (Which allows List@Level and Lacing) and Imperative blocks, which do not allow for List@Level or replication.


Thank you so much! Now it works! :slight_smile:

1 Like

You can create an associative block within an imperative, but you must define a function.