[Tutor] can someone explain to me Lambda functions/statements, preferably with examples, I don't get it

Steve Spicklemire steve@spvi.com
Wed, 23 Aug 2000 05:41:12 -0500 (EST)


Hi Charles,

Lambda is used to make a 'quick' function:

e.g., 

You could create a 'normal' function say foo:

def foo(x):
    return x - 3*x + 4*x**2

and then use foo in another situation where a function is required:

print map(foo, [1,2,3,4,5])

would result in: [2, 12, 30, 56, 90]

a shorter definition would be using lambda (same function)

bar = lambda x : x - 3*x + 4*x**2  

print map(bar, [1,2,3,4,5])

would produce the same result... 

of course, now you could just skip defining 'bar' and use
the function directly in the 'map'...

print map(lambda x : x - 3*x + 4*x**2, [1,2,3,4,5])

and get the same result again... 

Basically 

lamda x, y, z, .... : (single line fuction of x, y, z ...)

is equivalant to:

def foo(x, y, z, ...):
    return (same single line function of x, y, z, ...)

except the lambda version doesn't have (or need) a 'name'. 

does that help?
-steve