I have a function that will run a couple of times (in a recursive function, so with a 'while' condition). Everytime it runs, it will return a certain integer. So for example, the first time it runs it returns a 3, the second time a 5 and the third time a 9.
Now I need to save these returns in a list. So I thought to create a separate function that would take these values and store them. So the endstate I'm looking for is to have a list = [3,5,8].
B = [3,6,5,7,8,10]
def function_1(A):
for i in range(len(A)/2):
factor = A[2*i]
list_of_diagonals(factor)
return factor`
def list_of_diagonals(d):
factor_list = []
factor_list = factor_list.append(d)
return factor_list`
Now I would expect that print function_1(B) would produces [3,5,8] but instead it just produces 8.
I think it has something to do with the fact that I define factor_list=[] right at the start of the function, but how could I work around that?
factorfromfunction_1which holds the last iterated value ofA[2*i]yieldyet?whilestatement in your code anywhere. Just aforloop.yield. But I returnfactorin the first function, just to make sure I could use it in the second. I returnfactor_listin the second, so I could use it later, right?