Producing multiple items in a list comprehension

Yves Dorfsman yves at zioup.com
Fri May 23 10:23:28 EDT 2008


Peter Otten <__peter__ at web.de> wrote:

> The speed gain is significant. Why should I throw away useful information if
> I have it? 

My thinking was that it wasn't generic enough, and I was looking for a
solution that would work for more generic problem. I agree, I shouldn't
have used the world "elegant" here, more generic would have been better.

> I'd even be willing to convert one arbitrary iterable to a list
> to get the length information.

> $ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100" "aa = list(a);
> items=[None]*(2*len(aa)); items[::2] = aa; items[1::2] = b"
> 10000 loops, best of 3: 29.5 usec per loop

Yes, I like that, it is as generic as the reduce one.


> are more complicated than elegant. Not recommended.

You proved your point. Every book and webpage out there says that
functional solutions are faster than loops, that's why I tried hard not
to use a loop, and "reduce" is considered functional - I should have
timed it.


Yves.
http://www.SollerS.ca




More information about the Python-list mailing list