Search code examples
pythongenerator

How to clone a generator object?


Consider this scenario:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

walk = os.walk('/home')

for root, dirs, files in walk:
    for pathname in dirs+files:
        print os.path.join(root, pathname)

for root, dirs, files in walk:
    for pathname in dirs+files:
        print os.path.join(root, pathname)

We need to use the same walk data more than once. I've a benchmark scenario and the use of same walk data is mandatory to get helpful results.

I've tried walk2 = walk to clone and use in the second iteration, but it didn't work. How can I copy it? Is it ever possible?


Solution

  • You can use itertools.tee():

    walk, walk2 = itertools.tee(walk)
    

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