A = [[]]*2
A[0].append("a")
A[1].append("b")
B = [[], []]
B[0].append("a")
B[1].append("b")
print "A: "+ str(A)
print "B: "+ str(B)
Yields:
A: [['a', 'b'], ['a', 'b']]
B: [['a'], ['b']]
One would expect that the A list would be the same as the B list, this is not the case, both append statements were applied to A[0] and A[1].
Why?
A = [[]]*2creates a list with 2 identical elements:[[],[]].The elements are the same exact list.
So
appends both
"a"and"b"to the same list.B = [[], []]creates a list with 2 distinct elements.This shows that the two elements of
Aare identical:This shows that the two elements of
Bare different objects.