Have a look at this Python code:
a = [1, 2, 3]
b = [4, 5, 6]
c = [[a, b], [b, a]] # [[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [1, 2, 3]]]
c[0][0].append(99) # [[[1, 2, 3, 99], [4, 5, 6]], [[4, 5, 6], [1, 2, 3, 99]]]
Notice how modifying one element of c
modifies that everywhere. That is, if 99
is appended to c[0][0]
, it is also appended to c[1][1]
. I am guessing this is because Python is cleverly referring to the same object for c[0][0]
and c[1][1]
. (That is their id() is the same.)
Question: Is there something that can be done to c
so that its list elements can be safely locally modified? Above is just an example, my real problem has a list much more complicated, but having a similar problem.
(Sorry for the poorly formed question above. Python gurus please feel free to modify the question or tags to better express this query.)