using lambda to print everything in a list
Alex Martelli
aleaxit at yahoo.com
Sun Apr 29 07:33:23 EDT 2001
"Christian Tanzer" <tanzer at swing.co.at> wrote in message
news:mailman.988463382.11255.python-list at python.org...
"""
"Alex Martelli" <aleaxit at yahoo.com> wrote:
> My personal impression is that lambda is a (minor)
> nuisance. Naming the code fragment that you want
> to pass to some other function, by making it a
> nested function, seems clearer & handier to me.
What about using `map' to apply a method to each element in a list?
Like in:
map (lambda s : s.capitalize (), l)
I wouldn't call a loop clearer in this case, despite the lambda.
"""
Some of the equivalent alternatives are:
# 1. your lambda
L = map(lambda s: s.capitalize(), l)
# 2. list comprehension
L = [s.capitalize() for s in l]
# 3. plain append-loop
L = []
for s in l: L.append(s.capitalize())
# 4. special case, as you note, for string. stuff
L = map(string.capitalize, l)
# 5. a named local function
def toUpper(s): return s.capitalize()
L = map(toUpper, l)
There doesn't seem to be a huge clarity difference among
this set of possibilities, but my personal vote for clarity
order would be, roughly, 2-4-5-3-1. Others' tastes will no
doubt differ. But it seems to me that, even in a case as
simple as this one, lambda isn't really "pulling its weight"
as a language feature. It gets worse as soon as you need
to do anything substantial, IMHO...
Alex
More information about the Python-list
mailing list