I'm trying to create an iterator which lazily creates (potentially infinitely many) copies of an iterator. Is this possible?
I know I can create any fixed finite number of copies by simply doing
from itertools import tee
iter_copies = tee(my_iter, n=10)
but this breaks down if you don't know n ahead of time or if n is infinite.
I would usually try something along the lines of
from itertools import tee
def inf_tee(my_iter):
while True:
yield tee(my_iter)[1]
But the documentation states that after using tee on an iterator the original iterator can no longer be used, so this won't work.
In case you're interested in the application: the idea is to create a lazy unzip
function, potentially for use in pytoolz. My current implementation can handle a finite number of infinite iterators (which is better than plain zip(*seq)
), but not an infinite number of infinite iterators. Here's the pull request if you're interested in the details.