I want to make a copy of list in recursion function
def rec(I: List[int]) -> List[int]:
new_lst = I[:] # <- want to do this only on first call of rec in recursive stack
# ....(new_lst is edited)
rec(new_lst)
return new_lst
One solution is to wrap it in an initializer function, then call that initializer function instead:
def rec(I: List[int]) -> List[int]:
....(new_lst is edited)
rec(new_lst)
return new_lst
def startrec(I: List[int]) -> List[int]:
return rec(I[:])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With