I am trying to change an element in a Python list. Following the tutorial on https://www.programiz.com/python-programming/matrix, I came up with the code below.
matrix = [[0]*6]*3
print(matrix)
matrix[0][0] = 2
print(matrix)
Upon running the code, I received the following output:
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
[[2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0]]
Notice the last line of output, how the first element of every sub-list is set to 2
. How do I make it so that only the first element of the first list is changed.