The problem that I had to tackle was creating a function that recursively flattens a nested list.
The following below is my code.
What I don't understand is how the program keeps returning an empty list. I realize that I had to initialize an empty list and that could be the problem here but how else could I tackle this?
def flat_list(nested_lst, low, high):
new_lst = []
if low >= high:
return new_lst
if type(nested_lst[low]) is list:
for item in nested_lst[low]:
new_lst.append(item)
return flat_list(nested_lst, low+1, high)
else:
new_lst.append(nested_lst[low])
return flat_list(nested_lst, low+1, high)
def main():
lst = [[1], [[2, [3], 4], 5], 6]
print(flat_list(lst, 0, len(lst)-1))
main()
lowandhighare even relevant to the problem. You seem to be over-complicating it.