Skip to content

Instantly share code, notes, and snippets.

@eyaltrabelsi
Last active July 19, 2019 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyaltrabelsi/c70929bf7b75c24bd9b16dfb2d1d9230 to your computer and use it in GitHub Desktop.
Save eyaltrabelsi/c70929bf7b75c24bd9b16dfb2d1d9230 to your computer and use it in GitHub Desktop.
Python-tricks generator comprehension
# A generator comprehension is the lazy version of a list comprehension.
>>> m = (x ** 2 for x in range(5))
>>> m
<generator object <genexpr> at 0x108efe408>
>>> list(m)
[0, 1, 4, 9, 16]
>>> m = (x ** 2 for x in range(5))
>>> next(m)
0
>>> list(m)
[1, 4, 9, 16]
@danield137
Copy link

maybe use next(m) to show what generators are? of a function yielding the results?

@eyaltrabelsi
Copy link
Author

thhanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment