Create a List full of lists that are flattend


Hello Guys,

i know it looks a little bit messy, but that is exaclty what i want to fix. On the left side are parameters that i want in my list and the result should look exatly like the “List Create” node shows. In order to get the wanted result i needed to flatten almost every parameter because of the different levels in their lists. So my question is: is there a way to create a python script that flattens the lists and gives me a list that looks like the one from the “List Create” node?
many thanks in advance

Update: The python script is now returning the wanted result but still not flattend, any suggestions?

it’s already flattened from list level 3 to list level 2. Can you describe your expected result?

Wanted result:


Current result

If there’s just one item for each point the IN to the correct bit of the list.

So list_1 (this is a terrible variable name by the way) = IN[4][0][0][0]

I’d put something more like:

width = IN[0]
fire = IN[1]
opening = IN[2][0] #Whatever index you need

OUT = width, fire, opening

When you come to read this in a month or two you’ll be much happier. And anyone else ever reading it will not hate you.

1 Like

Thanks for your suggestions! :smiley: thats actual funny :rofl: let em quickly change tha names.
But even then i still got the problem, that for example “Ortbeton - bewehrt” ist in a list in a list… :slightly_frowning_face:

Is this what you’re looking for?


If so, here’s the code:

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def flatten(lst):
    flattened_list = []
    for item in lst:
        if isinstance(item, list):
            flattened_list.extend(flatten(item))
        else:
            flattened_list.append(item)
    return flattened_list
        

    
in1 = flatten(IN[0])
in2 = flatten(IN[1])
in3 = flatten(IN[2])

result = [in1,in2,in3]

    
OUT = result

On your screen grab you’re showing 1 item per list… Some are just nested.

Are you saying you’ve actually got more than 1 item per list?

ATM there is only 1 item per list, yes.
Thanks Luffy thats exactly what i wanted to do.

List.Flatten > List.Chop should also work for a node based approach.

1 Like

Yes, but in the first picture it was getting messy (i don’t want to exclude the possibility that it was my fault).

I dont know how to combine your script with mine, could you help pls?

Sorry should have included the location.

List.Create > List.Flatten > List.Chop

End result is two core nodes more than the Python node, May outperform the Python in many cases as it doesn’t need to spin up another virtual machine for Python, and will be easier to maintain over time as those nodes are exceptionally stable.

Move the definition to before your inputs.
change each input line to variableName = flattened(IN[*])

I dont know what im doing wrong :frowning:

@maldemu96 , here’s the code of your case:

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

#flatten the list to get them on one level
def flatten(my_list):
    flattened_my_list = []
    for item in my_list:
        if isinstance(item, list):
            flattened_my_list.extend(flatten(item))
        else:
            flattened_my_list.append(item)
    return flattened_my_list

#parametername and value by IN[]
hvactypecombined = flatten(IN[0])
penetrationtype = flatten(IN[1])
penetrationheight = flatten(IN[2])
penetrationwidth = flatten(IN[3])
penetrationdiameter = flatten(IN[4])
wallmaterial = flatten(IN[5])
walldepth = flatten(IN[6])
firerating = flatten(IN[7])
hvactype = flatten(IN[8])

#list of the parameters
my_list = [hvactypecombined, penetrationtype, penetrationheight, penetrationwidth, penetrationdiameter, wallmaterial, walldepth, firerating, hvactype]

OUT = my_list

you don’t need to print (OUT) because in Dynamo, “OUT” is already like print in Python.


Im sorry to continuesly bothering you but it till wont work :confused:

Can you show me the result of each input (from IN[0] to IN[8]) ?

i did use flatten only for some parameter so the result looks lie this:

If the list levels are going to be the same each time you’d save yourself a lot of lines by just pointing it to the right level as I suggested.

Feeding in 0 three times is pointless too. Just put the 0 in the OUT list.

All you need is something like:

OUT = 0 , IN[0][0] , IN[1][0], 0, IN[1][0][0] #or whatever level of nest you have... experiment until you understand it. 

Try feeding in one nested list and write a single line of code in the Python node only:

OUT = IN[0]

then try OUT = IN[0][0]

then try OUT = IN[0][0][0]

That’ll give you a grasp of what is going on.

If you’re adding zero a few times try

OUT = 0 , IN[0][0] , 0 , “here is my out”

1 Like