[Tutor] map, filter and lambda functions
Thu, 23 Aug 2001 11:01:34 +0200
On 0, email@example.com wrote:
> > Here's the new, shorter version:
> > checklist = string.split(filetext, "\n")
> > checklist = filter(lambda x: x != '', checklist)
> > checklist = map(lambda x: string.strip(x), checklist)
> > checklist = map(lambda x: string.upper(x), checklist)
The last two lambdas are just identity functions applied to another
function, and thus redundant.
> You could define functions:
> def isNotEmpty(x): return x != ''
> def stripitem(x): return string.strip(x)
> def upperitem(x): return string.upper(x)
Same for the last two here. You might as well write
stripitem = string.upper
Or just pass string.upper into the map in the first place :)
> You can always replace a lambda with a function in Python,
> but in this case the lambda is probably clearer by putting
> the action explicitly in the operation.
Except that the lambda does no action it all, so it's obfuscating :)