Generators, generator expressions, and loops
> On 16 Nov 2018, at 14:54, Steve Keller <keller at no.invalid> wrote:
> More elegant are generator expressions but I cannot think of a way
> without giving an upper limit:
> for i in (2 ** i for i in range(1000000)):
> which looks ugly. Also, the double for-loop (and also the two loops
> in the above exmaple, for + while in the generator) look unnatural,
> somehow, i.e. loop over all elements which are created by a loop.
> Is there a more beautyful way?
from itertools import count
for i in (2**n for n in count()):
In general itertools includes a lot of useful stuff, it's worth reading the whole docs page: https://docs.python.org/3/library/itertools.html <https://docs.python.org/3/library/itertools.html>