Where is the usage of (list comprehension) documented?
Ned Batchelder
ned at nedbatchelder.com
Mon Jan 15 06:41:46 EST 2018
On 1/14/18 9:57 PM, Dan Stromberg wrote:
> On Sun, Jan 14, 2018 at 3:01 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>> Hi,
>>
>> I see the following usage of list comprehension can generate a
>> generator. Does anybody know where this is documented? Thanks.
> Here's the (a?) generator expression PEP:
> https://www.python.org/dev/peps/pep-0289/
>
> Here's a presentation I put together on this and related topics a while back:
> http://stromberg.dnsalias.org/~strombrg/Intro-to-Python/Python%20Generators,%20Iterators%20and%20Comprehensions%202014.pdf
>
> FWIW, [a for a in range(2)] is a list comprehension; it's eager. And
> (a for a in range(2)) is a generator expression; it's lazy.
>
I really wish these were called generator comprehensions. I don't
understand why they are not.
[ 2*x for x in range(10) ] # makes a list
( 2*x for x in range(10) ) # makes a generator
--Ned.
More information about the Python-list
mailing list