flatten a level one list

Peter Otten __peter__ at web.de
Fri Jan 13 03:13:08 EST 2006


bonono at gmail.com wrote:

> David Murmann wrote:

>> > # New attempts:
>> > from itertools import imap
>> > def flatten4(x, y):
>> >     '''D Murman'''
>> >     l = []
>> >     list(imap(l.extend, izip(x, y)))
>> >     return l

>> well, i would really like to take credit for these, but they're
>> not mine ;) (credit goes to Michael Spencer). i especially like
>> flatten4, even if its not as fast as the phenomenally faster
>> flatten7.
>>
> Me too. And expand a bit on flatten4, I got this interesting result.
> 
> bonono at moresing:~/bonobo/psp$ python ~/lib/python2.4/timeit.py -s
> "import itertools; a=zip(xrange(1000),xrange(1000))" "l=len(a);
> li=[None]*l*2;li[::2]=range(1000); li[1::2]=range(1000)"
> 1000 loops, best of 3: 318 usec per loop
> 
> bonono at moresing:~/bonobo/psp$ python ~/lib/python2.4/timeit.py -s
> "import itertools,psyco; a=zip(xrange(1000),xrange(1000));li=[]"
> "filter(li.extend,a)"
> 1000 loops, best of 3: 474 usec per loop

For a fair comparison you'd have to time the zip operation.
 
> Still 50% slower but it has the advantage that it works on all kinds of
> sequence as they can be efficiently izip() together.

Creating a list via list/map/filter just for the side effect is not only bad
taste,

~ $ python -m timeit -s'a = zip([range(1000)]*2)' 'lst=[];ext=lst.extend'
'for i in a: ext(i)'
1000000 loops, best of 3: 1.23 usec per loop
~ $ python -m timeit -s'a = zip([range(1000)]*2)' 'lst=[];filter(lst.extend,
a)'
1000000 loops, best of 3: 1.63 usec per loop

it is also slower than an explicit loop. Don't do it.

Peter



More information about the Python-list mailing list