May-29-2017, 10:37 PM
>>> a = [[0,0]] * 3 >>> b = [[0,0] for i in range(3)] >>> a [[0, 0], [0, 0], [0, 0]] >>> b [[0, 0], [0, 0], [0, 0]] >>> a == b True >>> # Here "is" the clue >>> a is b False >>> [id(i) for i in a] [57010936, 57010936, 57010936] >>> [id(i) for i in b] [57074232, 56920944, 57010576]With
a = [[0,0]] * 3
you make 3 copy of list [0,0] that refer to same memory location(what id()
show)With
[[0,0] for i in range(3)]
or [[0,0], [0,0], [0,0]]
are making list that dos not refer to same memory location.When do
a = b
,both name tag to same object in memory.