def flatten(t):list = []for i in t:if(type(i) != list and type(i) != tuple):list.append(i)else:list.extend(flatten(i))return list
Here is the function that I've written to flatten a list or a tuple that looks something like
l = ((5, (1,2), [[1],[2]]))
when I do
flatten(l)
I expected to get a list
[5, 1, 2, 1, 2]
but I instead get
[5, 1, 2, [[1], [2]]]
I am not really sure why this happens because
else:list.extend(flatten(i))
this part of the code will retrieve the int inside the third element of the tuple which is [[1],[2]] by calling it recursively. What am I missing here?
Thanks