I have some lists of lists. The length and depth of each list varies in an unpredictable way. I want to apply a function to each of the bottom elements. For purposes of this question, we can say I just need to convert each int to a float.
I searched the site and found this: Python: How to loop a list of lists of varying depth?
However, when adapting this to my problem, it successfully accesses each element, but does not seem to transform the actual values. At the end, I still have a list of int's, when I want a list of floats.
def list_flatten(my_list):
for item in my_list:
if(isinstance(item,list)):
list_flatten(item)
else:
item = float(item)
return my_list
problem = [[[[1,1],[2,2],[3,3]],[[4,4],[5,5],[6,6]],[[7,7],[[8,8],[9,9]]]]]
print(list_flatten(problem))