I have a list of chars. I want to insert a string in between the chars at given index.
For that I wrote a function to insert the item (string) in the list in-place. The list inside the function is correct BUT it is NOT modifying the original list that I am passing. I believe Python List is mutable.
def insert_inplace(lst, idx, item_to_insert):
lst = lst[:idx] + lst[idx+1:]
lst[:idx] = lst[:idx]
lst[idx+1:] = lst[idx:]
lst[idx] = item_to_insert
print(lst)
# getting correct answer here
string_list = ["a", "b", "c", "d", "e"]
replacement = "new inserted item"
i = 2
insert_inplace(string_list, i, replacement)
# string_list is not getting modified