Python 2.0b1 List comprehensions are slow

Guido van Rossum guido at beopen.com
Tue Sep 12 14:07:43 EDT 2000


Another way to look at this is: the list comprehension is a really
nice notation to replace a for loop with a list.append() call, with
currently only a slight optimization.

Look at [x+12 for x in a] as a really nice way to spell

    L = []
    for x in a: L.append(x+12)

and then using L.

Ways to optimize this (in 2.1; as Tim explained 2.0 is too close to
release) might include:

- Preallocate the result list so that the append() can be faster.

- Speed up the for loop: currently Python for loops use a Python int
as the loop counter which is incremented using Python int object
calls.  This can be avoided.

I apologize for the misleading comment in the NEWS file; I should have
timed it!

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)



More information about the Python-list mailing list