0

I was experimenting with lists in python and found the following confusing.

>>> a = [1, 2, 3, 4]
>>> A = [a]*3
>>> A
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> a[2] = 45
>>> 
>>> A
[[1, 2, 45, 4], [1, 2, 45, 4], [1, 2, 45, 4]]
>>> A = a * 3
>>> A
[1, 2, 45, 4, 1, 2, 45, 4, 1, 2, 45, 4]
>>> a[2] = 3
>>> A
[1, 2, 45, 4, 1, 2, 45, 4, 1, 2, 45, 4]

First, I multiplied like this: A = [a]*3 and changed an element of a. A changed because it contained 3 references of a. Then, I multiplied like this: A = a*3 and changed the same element of a. Now, why did A not change, assuming that it contained 3 references of a, too?

W. Zhu
  • 755
  • 6
  • 16

0 Answers0