I am trying to creating a function that takes in a list of nested lists which in turn may have nested lists inside them. While it doesn't make sense to me why i should have to do such a thing my lab instructor insists its very useful in future project and in life as whole. here is my attempt to create the double recursive function that accomplishes such a functionality. I would appreciate it if you could point out whats it I am doing wrong and how to solve it.
def flatten_list(list_arg):
if not list_arg:
return []
elif isinstance(list_arg[0],list):
return [list_arg[0]] + flatten_list(list_arg[1:])
else:
return [list_arg[0]] + flatten_list(list_arg[1:])
print(flatten_list([1,2,[3,[4,[5,6]]]]))
Here is the output:
[1,2,[3,[4,[5,6]]]]
which basically means that the function doesn't do a thing and it just consumes some stack memory for a while and terminates with the same input. Oh before I forget this is functional programming so i am not allowed to change variables, produce side effects, or use loops in my function. it should be purely recursive with no variable change etc just like how a regular mathematical function works.