Apply function to list regardless of levels in python

How about handling any list structure:

#Function to handle any list structure
ProcessLists = lambda function, lists: [ProcessLists(function, item) if isinstance(item, list) else function(item) for item in lists]
ApplyFunction = lambda func, objs: ProcessLists(func, objs) if isinstance(objs, list) else [func(objs)]

#Funtion to unwrap
def Unwrap(item):
    return UnwrapElement(item)
    
#Preparing input from dynamo to revit 
if isinstance(IN[0], list):
	views = ProcessLists(Unwrap, IN[0])
else:
	views = Unwrap(IN[0])

#Define function to get view type
def getViewType(view):
	return view.ViewType

#OUTPUT
OUT = ApplyFunction(getViewType,views)
9 Likes