I implemented a (simple) python generator. Now, I want to make another one (from it), which will iterate through all values, but last one.
def gen(x): # Generate the interval [x, 10]
if x <= 10:
yield x
for v in gen(x + 1):
yield v
What would be the best way to accomplish that? Is it possible to alter the original generator using a decorator?
Using a temporary variable as a simple “queue”, you can forward elements from any iterator and keep the last one at the end:
or more generally (for generators up to x last elements):