removing list comprehensions in Python 3.0

Steven Bethard steven.bethard at gmail.com
Sat Jul 9 21:26:05 EDT 2005


Devan L wrote:
>>>>import timeit
>>>>t1 = timeit.Timer('list(i for i in xrange(10))')
>>>>t1.timeit()
> 
> 27.267753024476576
> 
>>>>t2 = timeit.Timer('[i for i in xrange(10)]')
>>>>t2.timeit()
> 
> 15.050426800054197
> 
>>>>t3 = timeit.Timer('list(i for i in xrange(100))')
>>>>t3.timeit()
> 
> 117.61078097914682
> 
>>>>t4 = timeit.Timer('[i for i in xrange(100)]')
>>>>t4.timeit()
> 
> 83.502424470149151
> 
> Hrm, okay, so generators are generally faster for iteration, but not
> for making lists(for small sequences), so list comprehensions stay.

Ahh, thanks. Although, it seems like a list isn't very useful if you 
never iterate over it. ;)

Also worth noting that in Python 3.0 it is quite likely that list 
comprehensions and generator expressions will have the same underlying 
implementation.  So while your tests above satisfy my curiosity 
(thanks!) they're not really an argument for retaining list 
comprehensions in Python 3.0.  And list comprehensions won't go away 
before then because removing them will break loads of existing code.

STeVe



More information about the Python-list mailing list