Restart generator when it is exhausted.

Duncan Booth duncan.booth at invalid.invalid
Tue Apr 28 12:41:50 CEST 2009

Lacrima <Lacrima.Maxim at> wrote:

> If it is not possible what are common techniques to use iterator or
> generator objects that allow restarting when it is needed?

The usual thing if you want to use the generator's output more than once  
would be to convert the generator to a list, then you can iterate over it 
as often as you want.

>>> a = ['a', 'b', 'c']
>>> g = (i for i in a)
>>> restartable = list(g)

If you want the output of the generator to potentially change each time you 
iterate then you need to create a new generator.

Duncan Booth

More information about the Python-list mailing list