Python Generators

Marius Gedminas mgedmin at gmail.com
Tue Mar 18 19:05:29 CET 2008


On Mar 16, 9:24 am, Matt Nordhoff <mnordh... at mattnordhoff.com> wrote:
> mpc wrote:
> > def concatenate(sequences):
> >     for seq in sequences:
> >         for item in seq:
> >             yield item
>
> You should check out itertools.chain(). It does this. You call it like
> "chain(seq1, seq2, ...)" instead of "chain(sequences)" though, which may
> be a problem for you.

Solved rather easily by chain(*sequences):

>>> from itertools import chain
>>> def concat(sequences):
...     return chain(*sequences)
...
>>> concat([[1,2], [3, 4], [5], [6, 7, 8]])
<itertools.chain object at 0xb7cffc0c>
>>> list(concat([[1,2], [3, 4], [5], [6, 7, 8]]))
[1, 2, 3, 4, 5, 6, 7, 8]


wondering if google groups will add a .sig or not-ly,
Marius Gedminas



More information about the Python-list mailing list