Created
August 10, 2022 11:54
-
-
Save blacksmithop/86f428333107fda6f8f382d3dd2c8bdd to your computer and use it in GitHub Desktop.
Infinite iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain | |
def gen_iterables(): | |
while True: | |
for i in range(1, 6): # memory equivalent to that used by `range` is consumed at any moment | |
yield range(i) | |
gen = chain.from_iterable(gen_iterables()) | |
for i in range(20): | |
print(next(gen)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment