There is no array type in python, but to emulate it we can use lists. I want to have 2d array-like structure filled in with zeros. My question is: what is the difference, if any, in this two expressions:
zeros = [[0 for i in xrange(M)] for j in xrange(M)]
and
zeros = [[0]*M]*N
Will zeros
be same? which one is better to use by means of speed and readability?
zeros = [[0]*M]*N
will NOT get you want you want because each row will be an instance, so modifying any column will change that column in all rows! if M and N are 3,zeros[0][1]=2
will result in[[0,2,0],[0,2,0],[0,2,0]]
[[0]*M]*N
)