At school we are studying recursion, but of course this is never obvious. We have to create a function that "flatten" the list. I already had to see something on the net, and I resolved mine in this way:
flattened_list = []
def flatten_list(ls=[]):
global flattened_list
for elem in ls:
if not isinstance(elem, list):
print("ADDING NO-LIST ELEMENT...")
flattened_list.append(elem)
else:
print("RECURSION...")
flatten_list(elem)
The problem is that flattened_list
is a global list, which you have to call exactly so, for the function to work. Can I improve this?