How to reset a generator object in Python
Codemia Knowledge Hub ·
You cannot reset a Python generator object back to the beginning. A generator is stateful: once it advances, its execution state moves forward until it is exhausted. For restartable iteration, create a new generator object, or wrap the logic in an iterable that produces a fresh generator each time.
Why generators cannot be rewound
A generator remembers its current execution point, local variables, and pending yield position, and Python has no API to rewind that state.
Here g is already partway through the function. There is no reset() method to send it back to the first yield.
Call the generator function again
The normal "reset" is to call the generator function again.
Generator functions are factories for generator objects. The function stays reusable even though each generator instance is single-pass.
Return a fresh generator from __iter__
If callers should be able to iterate repeatedly without recreating anything, use a class whose __iter__ returns a new generator.
The object is reusable because each iteration gets a fresh generator. The same idea applies if you store a generator as an object attribute and expect every method call to start from the beginning: store the generator function or the source data instead.
Cache the values when you need replay
If the computation is expensive and the result is finite, the right answer may be to store the generated values rather than reset anything.
This trades memory for replayability. It fits when the sequence is small or repeated access matters more than streaming; materializing an enormous generator into a list can cost a lot of memory.
itertools.tee does not rewind
itertools.tee splits one iterator into several independent iterators, but it is not a reset.
It duplicates iteration state forward from the current point. It cannot take an already-consumed generator back to the start.
Picking the right abstraction
Decide what you actually need:
- single-pass streaming
- repeatable iteration
- cached results for reuse
If repeatability is required, a bare generator object is usually the wrong thing to hand around. This matters most in APIs: returning a generator is ideal for streaming, but it surprises callers who assume they can iterate the result multiple times like a list.
Related reading
- Reshape your data either using array.reshape-1, 1 if your data has a single feature or array.reshape1, -1 if it contains a single sample
- Residual plot for residual vs predicted value in Python
- Result of GridSearchCV as table
- Results not reproducible with Keras and TensorFlow in Python
- Retrieve list of tasks in a queue in Celery
- Retrieve queue length with Celery (RabbitMQ, Django)
- Retrieve S3 file as Object instead of downloading to absolute system path
- Retrieving Dictionary Value Best Practices
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.