proposal: concatenation of iterators

Martin v. Loewis martin at v.loewis.de
Mon Aug 12 18:05:54 CEST 2002


Paul Rubin <phr-n2002b at NOSPAMnightsong.com> writes:

> Any thoughts?  Is there already some simple way to do this?

Depends on what "this" is. To implement +, you have to touch the
source code of each iterator, which is not simple. However, I find

def add_iter(*iters):
    for i in iters:
        for e in i:
            yield e
    raise StopIteration

a = iter((2, 3, 4))
b = iter((5, 6, 7))

for x in add_iter(a, b):  # concatenate two iterators
   print x,

quite simple already.

Regards,
Martin



More information about the Python-list mailing list