4 ms·
Just make sure that when you do this, you're not initializing it with mutable objects, e.g. lists. x = [[0]] * 2 print x[0], x[1] # prints [0] [0]
by daeken 15y ago
Just make sure that when you do this, you're not initializing it with mutable objects, e.g. lists.
x = [[0]] * 2
print x[0], x[1] # prints [0] [0]
x[0].append(1)
print x[0], x[1] # prints [0, 1] [0, 1]
That's because the objects inside the repeated list are simply references to the same object internally.