+ 2
Help recursive function (solved)
Goal: To flatten list : a= [1, [1,2,3], 2], [1,2] My function: def recusive_flat(x): r = [] for m in x: if m is iter: recursive_flat(m): else: r.append(int(m)) My goal is to loop through until every value is added as an integer.
3 Answers
+ 5
My fix:
Check existence of "__iter__" method.
https://code.sololearn.com/cd8oFew3A8y9/?ref=app
+ 4
madeline
First of, the a you provided is a Tuple, but since you said flatten list, I corrected it to a list.
So:
a = [1, [1,2,3], 2, [1,2]]
def sum_it(x):
flatten = [i if type(i)==int else sum(i) for i in a]
return sum(flatten)
print(sum_it(a))
+ 1
thank you.