1

First of all, really sorry I remember there should be a term for these two but I am really bad at terms.

Anyways, I am just playing around and at first I thought both of them should be giving the same result which YES it gave the same result but when I try to assign something into one of the index, that's when I found out there are differences, but I can't figure out.

Can someone please give me a hand on explaning the differences and why the result is like this?

e = [[0 for _ in range(3)] for _ in range(3)]
e[0][1] = 1
print(e, 'e')  # [[0, 1, 0], [0, 0, 0], [0, 0, 0]] e


d = [[0] * 3] * 3
d[0][1] = 1
print(d, 'd')  # [[0, 1, 0], [0, 1, 0], [0, 1, 0]] d

as seen, e is doing exact what I expected changing only index [0][1] to ` but as for d it's changing all. Why is this happening though?

Thanks in advance for any help and explanations

Dora
  • 6,776
  • 14
  • 51
  • 99

0 Answers0