Recursive list filling in python
Solution 1:
You could use the extend method:
def complex_func(a,b): return a+b
L = [1,2]
L.extend(complex_func(*L[-2:]) for _ in range(9))
print(L)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
If the operation is commutative, you could even start with an empty list by providing default values to the function arguments:
def complex_func(a=0,b=1): return a+b
L = []
L.extend(complex_func(*L[-2:]) for _ in range(10))
print(L)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Solution 2:
You can use range
.
start = 1
times = 9
res = list(range(start, start + times + 1))
print(res)