List of Objects being Changed when Original Object is Changed

Viewed 63

In Python, if I write:

a = [1]
b = 3 * [a]
a.append(2)
print a, b

Then, the output is:

[1, 2] [[1, 2], [1, 2], [1, 2]]

However, when I write:

a = [1]
b = 3 * a # notice the missing brackets here 
a.append(2)
print a, b

This turns out to be:

[1, 2] [1, 1, 1]

What is going on here?

2 Answers
Related