this will give you a clear idea: in l, all objects have the same identifier (), and all of them are mutable, so editing any of them will automatically change the others, since all of them simply refer to the same object with id = 18671936 and in m everything have different id (), so they are all different objects.
>>> l = [[]]*4 >>> for x in l: print(id(x)) 18671936 18671936 18671936 18671936 >>> m=[[],[],[],[]] >>> for x in m: print(id(x)) 10022256 18671256 18672496 18631696
So you should create your list as follows:
>>> a=[[] for _ in range(4)] >>> a[0].append(1) >>> a[2].append(5) >>> a [[1], [], [5], []]
source share