Feb-17-2021, 06:20 PM
The initial arrays are not the same. The first one creates a list, then puts several references to that list in another list. There's only two list objects here, the inside one and the outside one. If the inside list is modified, all three references to it are changed.
The second list creates 3 separate interior lists and puts them all in another list. There are four separate list objects that are independent. Changing one doesn't affect the others.
The second list creates 3 separate interior lists and puts them all in another list. There are four separate list objects that are independent. Changing one doesn't affect the others.
>>> h = 3 * [3*[0]] >>> print([id(x) for x in h]) [4327610368, 4327610368, 4327610368] # 3 references to the same object >>> h1 = [[0,0,0], [0,0,0], [0,0,0]] >>> print([id(x) for x in h1]) [4328654144, 4328656704, 4328678144] # 3 different objects