I tried to define a function on python to merge the strings on a given index range, from a list of strings. Here is my code:
def merge_some(L,i,j):
result=L[:]
merging=''
for k in range(i,j+1):
merging +=L[k]
result[i:j+1]=merging
return result
trial= ['a','b','c','d','e','f']
print(merge_some(trial, 1,3)) #index 3 included
The output should be:
['a','bcd','d','e','f']
The code should work fine, but if I run it, I just get back the original list, which is quite strange to me. Whilst I know there's plenty of methods to compute such function (e.g. using the .join() method instead of looping), I would like to ask if somebody has any idea of the cause of such weird behaviour. Any idea would be very much appreciated. Many thanks!
print()to see what you have in variables.result=result[:i]+[merging]+result[j+1:](Edit: this works too:result[i:j+1]=[merging])