Where is the usage of (list comprehension) documented?

Dan Stromberg drsalists at gmail.com
Sun Jan 14 21:57:48 EST 2018


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.

HTH



More information about the Python-list mailing list