I am asked to define a function that takes in a list and returns another list, all that while using recursion. However when I run the else command and I print the lst_
, the output shows that on every run the list contains a single element, instead of having the doubles added one by one.Also I try not to use append()
Thoughts?
def double(lst, lst_ = []):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_ = lst_ + lst[0:1]
print(lst_)
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
This is the output
[2]
[4]
[6]
[8]
[10]
[12]
[14]
[16]
[]