How to clone a Python generator object?

You can use itertools.tee():

walk, walk2 = itertools.tee(walk)

Note that this might “need significant extra storage”, as the documentation points out.

Leave a Comment