list problem

Alex Martelli aleax at aleax.it
Tue Sep 24 09:22:02 EDT 2002


Duncan Booth wrote:

> Alex Martelli <aleax at aleax.it> wrote in
> news:tCYj9.157570$pX1.5675986 at news2.tin.it:
> 
>>> If you are going to use reduce, then you would be better off writing:
>>> 
>>>    import operator
>>>    reduce(operator.add, x)
>> 
>> Maybe (marginally).  But timing things is often a good idea:
> 
> Apologies for not being clearer. I actually felt that using operator.add
> read more cleanly than using a lambda. I should have made it more obvious
> that was what I meant. I wouldn't particularly worry which is faster
> unless one was actually proven to be a bottleneck, at which point the OP
> might well find the best way to optimise would be to not end up with
> nested lists in the first place.

My apologies in return -- you're perfectly right in your overall
attitude, and I even agree with you that lambda isn't clearest.
I don't know why I focused on performance in answering this
thread -- must have been on my mind for other reasons.

I _do_ think that reduce(operator.__add__, ...) is the only
really frequent use of reduce, with such dominance that it might
even be worth having as a built-in -- clearest and fastest.  It
might even specialcase _strings_ in particular, turning the op
into a ''.join in this case... just musing...

But I did like the nested list comprehension better in this
case as an issue of style, anyway.  You DID write "If you are
going to use reduce", being impeccable on this point too.  So,
sorry again for appearing to pick on your post for no good reason.


Alex




More information about the Python-list mailing list