Say I have
x = [[0,0]]
y = x[-1]
Then why does
y[1] += 1
give
x = [[0,1]]
y = [0,1]
That is, I'm confused as to why it also changes what x is, even though I only specified something to do with y?
Say I have
x = [[0,0]]
y = x[-1]
Then why does
y[1] += 1
give
x = [[0,1]]
y = [0,1]
That is, I'm confused as to why it also changes what x is, even though I only specified something to do with y?
This makes sense if you think of a list as a mutable object in memory and you consider that you're modifying that list.
The variables are simply different names for that same list.
That code is equivalent to:
list_obj = [0, 0]
x = [ list_obj ] # a list with list_obj as
# its single element
y = list_obj # same as x[-1], just
# another name for list_obj
Then it's natural that you're simply modifying list_obj[1]
in both cases.