Re: flatten a list of list

2009-08-17 Thread Tim Cook
On Aug 16, 6:47 am, Terry terry.yin...@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) br

Re: flatten a list of list

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

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

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 Steven D'Aprano
On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote: On Sun, Aug 16, 2009 at 5:47 AM, Terryterry.yin...@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

Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 6:49 AM, Steven D'Apranost...@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, Terryterry.yin...@gmail.com wrote: Hi, Is there a simple way (the pythonic way) to flatten a list of list

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 timeit import Timer setup = L = [ ([None]*5000) for x

Re: flatten a list of list

2009-08-16 Thread Terry
, 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

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 terribly well. In my testing, it performs about

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 Pythonic, and while

Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 7:31 AM, Steven D'Apranost...@remove-this-cybersource.com.au 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

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 (or copy)

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 _ in xrange(%d) ] ... from itertools

Re: flatten a list of list

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

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) hth, Alan Isaac -- http

Re: flatten a list of list

2009-08-16 Thread Paul Rubin
Terry terry.yin...@gmail.com 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.org