12

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.

2
  • it's a variable for # of points
    – Charles L.
    Mar 18, 2011 at 1:41
  • 2
    If you're doing numerical work, I strongly recommend using a numerical package.
    – detly
    Mar 18, 2011 at 2:02

4 Answers 4

11

Maybe you should consider using NumPy. It seems like you're doing numerical work, which is what it's made for. This is the fastest so far, not including the import statement:

import numpy
Np = 80
zeroMatrix = numpy.zeros((Np, Np))

Times:

>python -m timeit -s "import numpy; Np = 80" "zeroMatrix = numpy.zeros((Np, Np))"
100000 loops, best of 3: 4.36 usec per loop

>python -m timeit -s "Np = 80" "zeroArray = [0]*Np" "zeroMatrix = [None] * Np" "for i in range(Np):" "  zeroMatrix[i] = zeroArray[:]"
10000 loops, best of 3: 62.5 usec per loop

>python -m timeit -s "Np = 80" "zeroMatrix = [[0] * Np for i in range (Np)]"
10000 loops, best of 3: 77.5 usec per loop

>python -m timeit -s "Np = 80" "zeroMatrix = [[0 for _ in range(Np)] for _ in range(Np)]"
1000 loops, best of 3: 474 usec per loop
1
  • 3
    +1 and of course numpy has a powerful set of array operations once you set up the array which are immensely faster than spinning your own later for python lists.
    – JoshAdel
    Mar 18, 2011 at 2:38
10

You could do this:

zeroMatrix = [[0] * Np for i in range(Np)]

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 append and avoids preallocating zeroMatrix.

zeroArray = [0] * Np
zeroMatrix = [zeroArray[:] for i in range(Np)]

My test results:

$ python -m timeit -s "Np = 80" "zeroMatrix = [[0] * Np for i in range(Np)]"
1000 loops, best of 3: 200 usec per loop
$ python -m timeit -s "Np = 80" "zeroArray = [0] * Np" "zeroMatrix = [None] * Np" "for i in range(Np):" "    zeroMatrix[i] = zeroArray[:]"
10000 loops, best of 3: 171 usec per loop
$ python -m timeit -s "Np = 80" "zeroArray = [0] * Np" "zeroMatrix = [zeroArray[:] for i in range(Np)]"
10000 loops, best of 3: 165 usec per loop
7
  • 1
    The second (IIRC) makes each element of zeroMatrix a copy of the same mutable list, so zeroMatrix[0][2] += 1 will affect all zeroMatrix[n][2] elements. (Unless they've changed that behavior for Python 3. I really need to upgrade already...) EDIT: Nevermind, you removed it.
    – Chris Lutz
    Mar 18, 2011 at 1:35
  • Mine is significantly faster than yours according to timeit. Though in python3 they are approximately equivalent. Though if you increase Np enough, mine still wins. Mar 18, 2011 at 1:35
  • 1
    @Charles - No, it doesn't. senderle had that code at one point, but took it out because it's wrong. See my earlier comment.
    – Chris Lutz
    Mar 18, 2011 at 1:45
  • 1
    @Charles L.: That will not work. As soon as you set one element in that matrix, the entire column that element is in will appear to be set to the same thing. Mar 18, 2011 at 1:45
  • 1
    I tested, and your version shows up as consistently very slightly slower than my version. I suspect which of ours is best depends on the Python version and/or hardware platform. Aug 19, 2011 at 16:33
5

This would probably be slightly more efficient:

zeroArray = [0]*Np
zeroMatrix = [None] * Np
for i in range(Np):
    zeroMatrix[i] = zeroArray[:]

What you would really like won't work the way you hope. This is because if you created Np copies of a list element using *, you get Np references to the same thing. For the 0 this isn't a big deal since you just get a new number when you add anything to it. But for lists you would end up with a matrix where as soon as you changed any element of a row, the entire column would change right along with it.

This way is the second fastest so far mentioned:

$ python3 -m timeit -s 'Np = 80' 'zeroArray = [0]*Np
zeroMatrix = [None] * Np
for i in range(Np):
    zeroMatrix[i] = zeroArray[:]'
10000 loops, best of 3: 72.8 usec per loop

$ python3 -m timeit -s 'Np = 80' 'zeroMatrix = [[0] * Np for i in range(Np)]'
10000 loops, best of 3: 85 usec per loop

$ python3 -m timeit -s 'Np = 80' 'zeroMatrix = [[0 for _ in range(Np)] for _ in range(Np)]'
1000 loops, best of 3: 566 usec per loop

I can't do my own timeit of the numpy-based solution as I don't have a numpy package for Python3 on my system. But it is very definitely faster by a significant margin.

8
  • Well, so much for trying to make the absolutely fastest one. That's really irritating. Mar 18, 2011 at 1:44
  • @Omnifarious - I win anyway :P
    – detly
    Mar 18, 2011 at 1:59
  • @detly - So you do. I'm not all that familiar with numpy. :-) Mar 18, 2011 at 2:06
  • @Omnifarious - I love it, because I might never have to use Matlab again.
    – detly
    Mar 18, 2011 at 2:10
  • @detly: See, I've never used matlab. :-) Mar 18, 2011 at 2:13
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.