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), list_of_list)
>
> br, Terry
Well,
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.org/mailman/listinfo/py
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
--
h
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 %
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 itertoo
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
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
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
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
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,
>
> >>> Is there a simple way (the pythonic way) to flatten a list
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
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 list?
>>> rather than my current solution:
>>>
>>> new_lis
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=[]
>> for l in list_of_list:
>> new_list.extend(l)
>>
>> or
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
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,y:x.extend(y), list_of_list)
#only ma
15 matches
Mail list logo