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.
Perhaps this is what you'd like?
zeroMatrix = [[0 for _ in range(Np)] for _ in range(Np)]
I'm not sure if this will provide a performance benefit (profile, as always) but I don't really know what you mean by "efficient." Other than avoiding the use of list.append
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With