I’m working on something that uses search, so each recursive call needs a separate copy of a list but with a single element replace. I’d like to do it (neatly) in one line, and so far I have this which goes in the recursive call:
[new_value if x == replace_index else my_list[x] for x in range(len(my_list))]
But is there a neater way to do this, potentially using some built in function I’m missing?
Maybe something like this:
or:
or:
In this case, I think I prefer the second option (probably is also the fastest one).