I know that slice operator does a shallow copy.
But when I used the slice operator on a list of integers and mutate the slice then the mutation was reflected in the original list. Since integers are immutable, the result confused me.
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Before slicing...")
print(x)
print("Slicing...")
x[1::2]=[0]*len(x[1::2])
print("After slicing...")
print(x)
x[2:2]=[1]
print(x)
The output was
Before slicing...
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Slicing...
After slicing...
[1, 0, 3, 0, 5, 0, 7, 0, 9, 0]
[1, 0, 1, 3, 0, 5, 0, 7, 0, 9, 0]
Since x[1::2]
and x[2:2]
does a shallow copy why does a change to it reflected in the original list?