shallow copy problem with values and references

49 views Asked by At

In python I have:

x = [1,2,3,[4,5,6]]
y = x[:]

x[3][0] = 9

so now:

x = [1,2,3,[9,5,6]]
y = [1,2,3,[9,5,6]]

but: x[0] = 99

then:

x = [99,2,3,[9,5,6]]

and:

y = [1,2,3,[9,5,6]]

how come when I change an element that's in a list of a list it's reflected in both x an y but when I change an element in a list they are not?

0

There are 0 answers