Flatten a two-level list --> one liner?

Sergio Correia sergio.correia at gmail.com
Wed Mar 7 20:06:59 EST 2007


 O_o

List comprehensions never cease to amaze me.

Thanks,


On 3/7/07, Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
> En Wed, 07 Mar 2007 21:41:16 -0300, Sergio Correia
> <sergio.correia at gmail.com> escribió:
>
> > I forgot to say: I've already checked the discussions about
> > recursive/iterative/etc flatten() functions that can sweep through a
> > list of arbitrary length, but that's really overkill for what I want.
> >
> > However, "sum(eggs, [])" or itertools seem to do it w/out the need for
> > a new function.
> >
> > Thanks a lot!
>
> Just for being (a little!) constructive, given your preconditions on the
> list, you can use:
> eggs = [item for sublist in spam for item in sublist]
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list