Your issue was overlapping variables and incorrect slicing. The solution:
def func1(b):
n = len(b)
a[:n] = [x**2 for x in b[:n]]
a = list(range(10))
func1(a[:3].copy())
print(a)
[0, 1, 4, 3, 4, 5, 6, 7, 8, 9, 10]
I also noticed that in the function, the b[:n] is redundant. You can simply put it as b.
The above solution is only if it sliced from the start. I also coded a solution for it to be sliced from the middle, was waiting for the weekend to post it.
def func1(b):
n1 = len(b)
n2 = len(a)
if n1<=n2:
first_element = b[0]
last_element = b[n1-1]
first_element_index = a.index(b[0],0,n2)
last_element_index = a.index(b[n1-1],0,n2)
if last_element_index - first_element_index==n1-1:
a[first_element_index+1:last_element_index+1] = [x**2 for x in b]
def rec(first,last,start,end,n1,n2):
while start<=n2 and end<=n2:
if a.index(last,start,end)-a.index(first,start,end)==n1-1:
a[a.index(first)+1:a.index(last)+1] = [x**2 for x in b]
else:
return rec(first,last,a.index(first)+1,a.index(last)+1,n1,n2)
a[:]=[x**2 for x in b]
else:
a[:] = [x**2 for x in b]
a = [1,8,9,4,5,7,2,6]
func1(a[6:8])
print(a)
[1, 8, 9, 4, 5, 7, 2, 4, 36]
The code is longer than usual because I confirm subset of a ..it can be shortened if subset is guaranteed. Similarly, it can be lengthened and made more robust by adding more checks and validations.