Sayanta Seth
Sayanta Seth

Reputation: 77

Feeding a function output back to it as input parameter

I am writing a simple function and I want to iteratively run it so that it gets fed its own output, performs some operations and continues to do so until a certain number of times.

I have tried the following:

def optimize(lst):
    A = lst[0]
    B = lst[1]
    C = lst[2]
    # perform some operation, for example,
    A = A+1
    B = B+1
    C = C+1
    new_lst = [A,B,C]
    lst[:] = new_lst # this overwrites the original param with the new output
    print(new_lst)
    return(new_lst)

for i in range(3):
    optimize([1,2,3])

        

This just repeats for three times with the same input, i.e., 1,2,3 and prints [2,3,4]. It's not getting updated in each iteration. I mean it should take [2,3,4] and spit out [3,4,5] and so on. I know I am missing a very simple concept here. Kindly help. Thank you!

Note: The operations in my function is much more complicated. I chose to oversimplify it for representation purpose.

Upvotes: 2

Views: 133

Answers (3)

Mark Tolonen
Mark Tolonen

Reputation: 177715

Calling optimize([1,2,3]) creates a new [1,2,3] list each time it is called. The function is mutating the passed-in list, but it is thrown away each iteration and the return value wasn't used.

If you want to mutate the input list, you need to create the list once:

def optimize(lst):
    lst[:] = [x + 1 for x in lst]  # mutate in-place, just for illustration

data = [1,2,3]

for _ in range(3):
    optimize(data)
    print(data)

Output:

[2, 3, 4]
[3, 4, 5]
[4, 5, 6]

Upvotes: 0

Kurt
Kurt

Reputation: 1748

You just need to capture the value returned by the function and then pass it back to the function next time

data = [1,2,3]
for i in range(3):
    data = optimize(data)

Edit to add:

You don't need the "overwrite original list" operation because you are returning the new list rather than updating in-place. Also, the parentheses around the return value are meaningless.

def optimize(lst):
    A = lst[0]
    B = lst[1]
    C = lst[2]
    # perform some operation, for example,
    A = A+1
    B = B+1
    C = C+1
    new_lst = [A,B,C]
    print(new_lst)
    return new_lst

Upvotes: 3

Cardstdani
Cardstdani

Reputation: 5223

You need to call the function again on the return statement. Also, it's necessary to have a base case where the function should stop being executed. In this case, I limited the value of A to 15, so it will only perform 15 function executions before reaching the base case:

def optimize(lst):
    A = lst[0]
    if A > 15:
        return
    B = lst[1]
    C = lst[2]
    # perform some operation, for example,
    A = A+1
    B = B+1
    C = C+1
    new_lst = [A,B,C]
    lst[:] = new_lst # this overwrites the original param with the new output
    print(new_lst)
    return optimize(new_lst)

optimize([1,2,3])

Output:

[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]
[9, 10, 11]
[10, 11, 12]
[11, 12, 13]
[12, 13, 14]
[13, 14, 15]
[14, 15, 16]
[15, 16, 17]
[16, 17, 18]

Upvotes: 0

Related Questions