You can use the functions provided by itertools to take the generator and create a new generator.
For example, you can use takewhile until the predicate is executed, and chain in a new series of values.
Take a look at the documentation for other examples, including ifilter , dropwhile and islice , to name a few.
source share