1

In the following code b1 and b2 give the same output

b1 = [[-1]*3 for _ in range(3)]
b2= [[-1] * 3] * 3

Output

b1 = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]

b2 = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]

But after performing a simple operation -

i = 0
j = 0

b1[i][j] = 1
b2[i][j] = 1

Output

b1 = [[1, -1, -1], [-1, -1, -1], [-1, -1, -1]]

b2 = [[1, -1, -1], [1, -1, -1], [1, -1, -1]]

Why is the first element in every row getting changed in b2 on changing just one value?

back2back
  • 33
  • 6

0 Answers0