can anyone explain what's happening behind this? I tried to search for similar behavior but most of the questions on iterating nested lists
r1=[0 for i in range(3)]
result=[r1 for j in range(3)]
result[0][0]=4 # it will edit all the 0th elements of the sublists
print(result)
r1=[0 for i in range(3)]
result=[r1 for j in range(3)]
result[1][0]=4 # it will edit all the 0th elements of the sublists
print(result)
r1=[0 for i in range(3)]
result=[r1 for j in range(3)]
result[0][1]=4 # it will edit all the 1st elements of the sublists
print(result)
result = [[0 for x in range(3)] for y in range(3)]
result[0][0]=4
print(result) # it will edit only the first element of first sublist
the output is
[[4, 0, 0], [4, 0, 0], [4, 0, 0]]
[[4, 0, 0], [4, 0, 0], [4, 0, 0]]
[[0, 4, 0], [0, 4, 0], [0, 4, 0]]
[[4, 0, 0], [0, 0, 0], [0, 0, 0]]