I'm creating an array with nested arrays of arrays in which which all the elements are 1, such as:
[ [[1, 1], [1, 1]],
[[1, 1], [1, 1]] ]
When I run the following code I expect to have changed only a single element to 555, leaving all others as 1.
mesh = [[[1] * 2] * 2] * 2
mesh[0][0][1] = 555
print(mesh)
# same as above but trying to access a different element
mesh = [[[1] * 2] * 2] * 2
mesh[1][1][1] = 555
print(mesh)
So I want:
[[[1, 555], [1, 1]], [[1, 1], [1, 1]]]
[[[1, 1], [1, 1]], [[1, 1], [1, 555]]]
Instead, I'm getting the following:
[[[1, 555], [1, 555]], [[1, 555], [1, 555]]]
[[[1, 555], [1, 555]], [[1, 555], [1, 555]]]
So, not only does it not do what I expect, different code seems to produce the same output. Please explain why this is happening and, if possible, what code I should use instead. Thank you.