0
lst1 = [[0 for x in range(3)] for x in range(3)]
lst2 = [[0]*(3)]*(3)
print(lst1)
print(lst2)
for i in range(1, 3):
    for j in range(1, 3):
        lst1[i][j] = 1
        lst2[i][j] = 1
print(lst1)
print(lst2)

Why I am getting different output? But it's the same I have done in different ways. Can anyone help? I am getting result given below,

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 1, 1], [0, 1, 1]]
[[0, 1, 1], [0, 1, 1], [0, 1, 1]]
  • array index starts with `0` so try `range(0, 3)` – tttony Sep 11 '21 at 01:27
  • Does [this](https://stackoverflow.com/questions/4230000/creating-a-2d-matrix-in-python?noredirect=1&lq=1) answer your question? The two list creation syntaxes you write are fundamentally different – kcsquared Sep 11 '21 at 01:35

0 Answers0