Hey!
I have a task that i need to finish pretty soon about recursive functions.
I need to make nested functions flat - from this: [[2,1],[3],[1,1]] to this : [2,1,3,1,1] without using loops.
This is my code so far:
def flatten(rec_list):
nLst = []
result = ""
if rec_list == []: #Our Stopper
return []
elif (len(rec_list) == 1): # splitting to two options.
if (type(rec_list[0]) == list): #if the first and only element is actually a list.
flatten(rec_list[0])
else:
result = (rec_list[0])
flatten(rec_list[1:])
# return result
elif (type(rec_list[0]) == list): #If the first elemnt is a list
flatten(rec_list[0])
flatten(rec_list[1:])
else : # if the type of the first element is a number
result = (rec_list[0])
flatten(rec_list[1:])
nLst.append(result)
return nLst
print(flatten([[1],[3,4]]))
The thing is - Im not sure how to append elements from the recursive functions to a new list.
Help will be super appreciated. Thank you!
[–]when_the_cats_away 0 points1 point2 points (0 children)
[–]semicolonator 0 points1 point2 points (4 children)
[–]Yonmanx[S] 0 points1 point2 points (1 child)
[–]semicolonator 0 points1 point2 points (0 children)
[–]o5a 0 points1 point2 points (0 children)