Code:
from array import *
import json
s = [[0]*5]*5
list1 = s
d = json.dumps(s)
list2 = json.loads(d)
list1[0][0] = 1
list2[0][0] = 1
print(list1)
print(list2)
This outputs:
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Why does it not output the same? Why does list1 set the entire column to value one rather than only index 0,0 ??? Any explanation, is this a bug or a feature?