Accessing nested lists using a python script

Hi All,

I have a dynamo script in which I have organized pipe fabrication information into nested lists.
I want to access specific information in the nested list to organize the data to export to excel in a very specific format for the fabrication shop.
How do I best access nested list information using a python script in dynamo?

Look at using List Comprehensions to access nested data

data = [[461, 162, 64, 63], [100, 100, 10, 10], [888, 512, 888, 512]]

print([i[2] for i in data])
# [64, 10, 888]
2 Likes

Thanks for the reply Mike.

How do I use the print fuction within a dynamo script though?
My understanding was I can only output data from a python node by using the OUT = [variableA], [ETC]

Is it possible to let a variable equal the print function (like below example)?

VairableA = print(i[2] for i in data])

Or am I missing something?
Thanks again for the assistance

Make an empty list, and append objects to it. In principle that ‘prints’ an outcome developed within the Python node back to Dynamo canvas.

Print is really moreso for use outside Dynamo in Python IDE’s etc.

To access objects within a list you can use the ‘for’ loop statement to work your way through the list structure. You can then work through sub-lists by nesting more for loops, and so on so forth.

If you drag that bit up you can see the print stuff. :slight_smile:

2 Likes

I’m a bit of a noob to python so apologies for the silly questions but how do i write to an empty list without getting a “out of index” error?

my_list = [ ] 

my_list.append("my string")
my_list.append(101)

OUT = my_list

image

1 Like

Thanks Alien, hadnt been using .append and think that was causing problems in the loop

Will give this a go :slight_smile:

Just have to work out how to do it for a list in a list in a list in a list now :stuck_out_tongue:

You just nest the list.

2 Likes

I was providing a basic example of a list comprehension rather than a dynamo python solution. However you have come to the right place with peeps like @GavinCrump and @Alien here to help!

3 Likes