substituting list comprehensions for map()

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Nov 2 22:32:34 EST 2009


On Tue, 03 Nov 2009 09:14:05 +1100, Ben Finney wrote:

> J Kenneth King <james at agentultra.com> writes:
> 
>> Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> writes:
>>
>> > from operator import add
>> > map(add, operandlist1, operandlist2)
>>
>> This is the best solution so far.
> 
> Strange to say it's a solution, when it doesn't solve the stated
> problem: to replace use of ‘map()’ with a list comprehension.

In context, I wasn't giving that as a replacement for map(), but as a 
replacement for map-with-a-lambda.


>> I understand the OP was asking for it, but list comprehensions aren't
>> the best solution in this case... it would just be line noise.
> 
> I disagree; a list comprehension is often clearer than use of ‘map()’
> with a lambda form, and I find it to be so in this case.


You obviously don't do enough functional programming :)

Apart from an occasional brain-fart where I conflate map() with filter(), 
I find them perfectly readable and sensible. The only advantages to list 
comps are you can filter results with an if clause, and for simple 
expressions you don't need to create a function. They're non-trivial 
advantages, but for me readability isn't one of them.


-- 
Steven



More information about the Python-list mailing list