1

can you please someone explain to me the diffrence between the 2 statements:

l = [[None] * 3] * 4
for i in range(4):
    for j in range(3):
        if i == 0:
            l[i][j] = 0
print(l)


M = [[None] * 3 for _ in range(4)]
for i in range(4):
    for j in range(3):
        if i == 0:
            M[i][j] = 0
print(M)

the output:

[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] # first statement
[[0, 0, 0], [None, None, None], [None, None, None], [None, None, None]] # second statement
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96

0 Answers0