List with unknown depth

just curious is it possible to loop if the input list is unknown depth:
e.g.
image
this is with fourth depth, but I want a function to work on various depth i.e. fifth, sixth…

is it possible?

Hi @newshunhk

in my opinion, the easiest way is to build a recursive function like below. Remember that by calling the function again the function run before hasn’t stopped running. It means It takes a lot of memory and if you have a big dataset you can run out of memory. Alternatively, a while loop could be used instead.

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

input_list = IN[0]

output_list = []

def iter_func(input_list):
	if any(isinstance(item, list) for item in input_list):
		for item in input_list:
			if isinstance(item, list):
				iter_func(item)
			else:
				output_list.append(item)
	else:
		if len(input_list) > 0:
			for item in input_list: output_list.append(item)

iter_func(input_list)

OUT = output_list
2 Likes

it works perfectly! thank you! :stuck_out_tongue_closed_eyes:

1 Like