Simon Brunning wrote:

> On Apr 5, 2005 2:04 AM, Raymond Hettinger <[EMAIL PROTECTED]> wrote:
>> [Steven Bethard]
>> > So do I read this right in preferring
>> >      [<x> for <y> in <z>]
>> > over
>> >      list(<x> for <y> in <z>)
>> 
>> Yes!
> 
> Why? (Serious question. I'm sure that you have a good reason - I just
> can't figure out what it is.)
> 
> The generator expression has the advantage of not leaking references
> into the enclosing namespace. What's advantage of the list comp?
> 
The list comprehension is about 15-20% faster according to timeit.py:

C:\Python24\Lib>..\python.exe timeit.py -s "t = range(1000)" "[ x for x in t]"
10000 loops, best of 3: 116 usec per loop

C:\Python24\Lib>..\python.exe timeit.py -s "t = range(1000)" "list(x for x in 
t)"
1000 loops, best of 3: 144 usec per loop

C:\Python24\Lib>..\python.exe timeit.py -s "t = range(100000)" "[ x for x in t]"
10 loops, best of 3: 13.9 msec per loop

C:\Python24\Lib>..\python.exe timeit.py -s "t = range(100000)" "list(x for x in 
t)"
10 loops, best of 3: 16.3 msec per loop

Alternatively you could just regard the list comprehension as having 
less clutter on the screen so it may be clearer.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to