I had recently visited the question List of lists changes reflected across sublists unexpectedly
After seeing it, I decided to try out and experiment with the things I found there to understand how it worked in general, but I've ran into a confusing issue that has left me stumped and confused.
Here is the code:
x = [1]*4
y = x*2
print(x,y) #[1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1]
x[0] = 7
print(x,y) #[7, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1]
x = [1]*4
y = [x]*2
print(x,y) #[1, 1, 1, 1] [[1, 1, 1, 1], [1, 1, 1, 1]]
x[0] = 7
print(x,y) #[7, 1, 1, 1] [[7, 1, 1, 1], [7, 1, 1, 1]]
Essentially, the elements within y only change when x is changed, if y is defined as [x]\*2, not when y is defined as x\*2. That is, only when I define y as x nested within a list replicated twice, it responds to changes I make in x.
Is there a reason why it only happens when y is assigned the value of x nested within a list?