[Tutor] List comprehension question

Wayne Werner waynejwerner at gmail.com
Mon Nov 8 01:41:48 CET 2010


On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts <hugo.yoshi at gmail.com> wrote:

> <snip>
> here's a list comprehension
> >>> a = [x*2 for x in range(10)]
> >>> a
> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>
> here's the equivalent generator expression:
> >>> a = (x*2 for x in range(10))

 <snip>


Since you're talking about generators and efficiency, it's probably a good
idea to point out that range is only a generator in python 3.x. In python
2.x it creates a list.

Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101107/608324c9/attachment.html>


More information about the Tutor mailing list