flatten a list of list

Terry terry.yinzhe at gmail.com
Sun Aug 16 13:22:00 CEST 2009


On Aug 16, 6:59 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Sun, Aug 16, 2009 at 6:49 AM, Steven
>
>
>
>
>
> D'Aprano<st... at remove-this-cybersource.com.au> wrote:
> > On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote:
> >> On Sun, Aug 16, 2009 at 5:47 AM, Terry<terry.yin... at gmail.com> wrote:
> >>> Hi,
>
> >>> Is there a simple way (the pythonic way) to flatten a list of list?
> >>> rather than my current solution:
>
> >>> new_list=[]
> >>> for l in list_of_list:
> >>>    new_list.extend(l)
>
> >>> or,
>
> >>> new_list=reduce(lambda x,y:x.extend(y), list_of_list)
>
> >> #only marginally better:
> >> from operator import add
> >> new_list = reduce(add, list_of_list)
>
> > Surely that's going to be O(N**2)?
>
> The OP asked for "simple", not "best", "most proper", or "fastest". My
> comment was intended to mean that the code was marginally *simpler*,
> not faster.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

Well, if possible, I'd like not only to know a simple solution, but
also the 'best', the 'most proper' and the 'fastest':-)

If they are not the same.

br, Terry



More information about the Python-list mailing list