I want to declare a list in python 2.7 and initialize it with 10 lists, each containing 10 elements, all initialized to 0. So its a list which contains 10 lists. I do it as shown below.
l = [[0] * 10] * 10
After doing this, when I try to assign a value to any element within any of the 10 lists, the assignment is reflected across all other lists. i.e
l[0][0] = 1
print l[0] --> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
print l[1] --> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
As seen above, the assignment of 1 to the first element of list at index 0 is reflected in the list at index 1 too. This is case for all the other 10 lists.
However, if I do the below
for i in range(10):
l.append([0] * 10)
This does not happen. I think this has got to do with the "*" operator, but I do not know the exact reason. I would appreciate it if anybody can help me understand this behavior.