I am creating a list of lists using this code:
zeroArray = [0]*Np
zeroMatrix = []
for i in range(Np):
zeroMatrix.append(zeroArray[:])
Is there a more efficient way to do this? I’m hoping for something along the lines of zeroArray = [0]*Np; zeroMat = zeroArray*Np but can’t find anything similar.
You could do this:
Update: Well if we’re going to make it into a race, I’ve found something faster (on my computer) than Omnifarious’ method. This doesn’t beat numpy of course; but this is all academic anyway right? I mean we’re talking about microseconds here.
I think this works because it avoids
appendand avoids preallocatingzeroMatrix.My test results: