Suppose I have an iterator, and I want to add some elements before or after it. The only way I can think of to do this is to use an explicit loop:
def myiter(other_iter):
yield "First element"
for item in other_iter:
yield item
yield "Last element"
Is there a better or more efficient way to do this? Is there a function with a name like yield_items_from that can be used like this?
def myiter(other_iter):
yield "First element"
yield_items_from(other_iter)
yield "Last element"
Edit:
Ok, I oversimplified my example. Here's a better one:
Suppose I have an iterator other_iter that returns an ascending sequence of nonnegative integers. I want to return an iterator that counts up from zero, returning 1 for numbers returned by other_iter and 0 otherwise. For example, if other_iter yields [1,4,5,7], I want to yield [0,1,0,0,1,1,0,1]. Is there an efficient and readable way to do this?