Re: flatten a list of list

2009-08-17 Thread Tim Cook
On Aug 16, 6:47 am, Terry 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

Re: flatten a list of list

2009-08-16 Thread Paul Rubin
Terry writes: > 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) from itertools import chain new_list = list(chain(list_of_list)) -- http://mail.python.or

Re: flatten a list of list

2009-08-16 Thread Alan G Isaac
On 8/16/2009 5:47 AM Terry apparently wrote: > 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) new_list = list(xi for lst in list_of_list for xi in lst)

Re: flatten a list of list

2009-08-16 Thread Francesco Bochicchio
On Aug 16, 1:25 pm, Steven D'Aprano wrote: ... > Chris' suggestion using itertools seems pretty good: > > >>> from timeit import Timer > >>> setup = """\\ > > ... L = [ [None]*5000 for _ in xrange(%d) ] > ... from itertools import chain > ... """>>> Timer("list(chain.from_iterable(L))", setup %

Re: flatten a list of list

2009-08-16 Thread Scott David Daniels
Steven D'Aprano wrote: On Sun, 16 Aug 2009 02:47:42 -0700, Terry wrote: Is there a simple way (the pythonic way) to flatten a list of list? Chris' suggestion using itertools seems pretty good: from timeit import Timer setup = """\\ ... L = [ [None]*5000 for _

Re: flatten a list of list

2009-08-16 Thread Bearophile
Chris Rebert: > 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. Yep, the OP has asked for simple code. But often this is not the right way to solve this situation. A better way is to create

Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 7:31 AM, Steven D'Aprano wrote: > On Sun, 16 Aug 2009 06:59:52 -0400, Chris Rebert wrote: >>> 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

Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 06:59:52 -0400, Chris Rebert wrote: >> 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. Fair enough, but he also asked for Pythoni

Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 02:47:42 -0700, Terry 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) I don't think that scales ter

Re: flatten a list of list

2009-08-16 Thread Terry
On Aug 16, 6:59 pm, Chris Rebert wrote: > On Sun, Aug 16, 2009 at 6:49 AM, Steven > > > > > > D'Aprano wrote: > > On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote: > >> On Sun, Aug 16, 2009 at 5:47 AM, Terry wrote: > >>> Hi, > > >

Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 12:03:53 +0200, Michael Fötsch wrote: > Terry wrote: >> Is there a simple way (the pythonic way) to flatten a list of list? > > This is probably the shortest it can get: > > sum(list_of_lists, []) That's also O(N**2). >>> from

Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 6:49 AM, Steven D'Aprano wrote: > On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote: >> On Sun, Aug 16, 2009 at 5:47 AM, Terry wrote: >>> Hi, >>> >>> Is there a simple way (the pythonic way) to flatten a list of

Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote: > On Sun, Aug 16, 2009 at 5:47 AM, Terry wrote: >> Hi, >> >> Is there a simple way (the pythonic way) to flatten a list of list? >> rather than my current solution: >> >> new_list=[] >> f

Re: flatten a list of list

2009-08-16 Thread Michael Fötsch
Terry wrote: Is there a simple way (the pythonic way) to flatten a list of list? This is probably the shortest it can get: sum(list_of_lists, []) Kind Regards, M.F. -- http://mail.python.org/mailman/listinfo/python-list

Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 5:47 AM, Terry 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

flatten a list of list

2009-08-16 Thread Terry
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) br, Terry -- http://mail.python.org/mailman/listinfo/python-list