proposal: concatenation of iterators

Andrew Koenig ark at
Mon Aug 12 17:56:05 CEST 2002

Paul> It would sure be useful sometimes to be able to say
Paul>   a = iter((2, 3, 4))
Paul>   b = iter((5, 6, 7))

Paul>   for x in a + b:  # concatenate two iterators
Paul>      print x,

Paul> and get:

Paul>   2 3 4 5 6 7

Paul> The implementation is obvious: a+b would just be a generator that
Paul> cycles through a and then b.  Easy enough to code up with new-style
Paul> classes, but it seems natural enough that it should be built into
Paul> normal iterators.

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

        def concat(a, b):
                for x in a: yield x
                for x in b: yield x

So I guess the question is whether you want to include __add__ and
__radd__ in the iterator protocol, or whether you want to make built-in
+ capable of detecting reliably whether its operands are iterators.

Andrew Koenig, ark at,

More information about the Python-list mailing list