1

I'm new to Python. I have created two nested lists with the following code:

m = [[0,0,0],[0,0,0],[0,0,0]]
n = [[0]*3]*3

If I'm not mistaken, these are two different ways to create the same list, so m and n should be identical. But when I try to modify one element, say:

m[1][0] = 4
n[1][0] = 4

What I'm getting is:

m = [[0, 0, 0], [4, 0, 0], [0, 0, 0]]
n = [[4, 0, 0], [4, 0, 0], [4, 0, 0]]

In m I just modify the m[1][0] element, as I expected. But in n the first element of all the lists are modified.

Why am I getting different results? Are m and n really identical?

And why the n list is behaving like this?

J-B
  • 11
  • 1

0 Answers0