Reset Generator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Reset Generator

What is the proper way to reset a generator, such as if you want to reuse the same generator in two separate areas of your code? ... please assume that combining those two separate uses into one is not feasible/desired.

28th Jan 2018, 4:51 AM
N. Förster
N. Förster - avatar
2 Answers
+ 7
It seems you can use itertools.tee() to make a second version of the generator: y = FunctionWithYield() y, y_backup = tee(y) for x in y: print(x) for x in y_backup: print(x) from https://stackoverflow.com/q/1271320
28th Jan 2018, 8:37 AM
David Ashton
David Ashton - avatar
+ 2
Thanks!
30th Jan 2018, 1:45 AM
N. Förster
N. Förster - avatar