### Thread: Let's get those lambdas straight

1. #### Let's get those lambdas straight

Ok, now, I've tried my best to get familiar with them, but I just can't understand what the HECK a lambda does. I've looked into every book I could manage to find, the Python tutorial...

So ? Help me please. I feel like a Ph.D. in math who can't add two numbers. Kinda.
2. A lambda is something python borrowed from functional programming languages. Basically, a lambda function is sort of like a regular function, except it does not have a name. Actually, it is more correct to call it as a lambda expression rather than a function. This means, a lambda can appear in the middle of a statement unlike a def statement.

So where would you use this? Well, it comes in handy, if you're planning to write quick event handlers and don't want to declare a separate function. Another classic use is when you have a list of items and want to apply a transform to all items in the list. For example, let's say you want to square all items in a list. The non-lamdba method would be to declare a function (we call it square()) and pass it as one of the arguments to the map() function, something like this:
Code:
```def square(x):
return x * x

list = [1, 2, 3, 4]
list2 = map(square, list)
print list2```
If you use lambda expressions, there's no need to declare a separate function. Simply declare a lambda expression within the map function:
Code:
```list = [1, 2, 3, 4]
list2 = map(lambda x: x * x, list)
print list2```
HTH
3. Sweet explination scorpy, damn sure i couldn't have done any better! Anyway, a simple example which uses filter() and lambda to return all the odd and even numbers in a list..

>>> l = range(50)
>>> filter(lambda x: x % 2, l)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
>>> filter(lambda x: not x % 2, l)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
>>>

and another which returns prime numbers up to 25 (I've borrowed the syntax from syntax thingy x % 2 != 0 amd x % 3 != 0)

>>> filter(lambda x: x % 2 != 0 and x % 3 != 0 and x < 25, l)
[1, 5, 7, 11, 13, 17, 19, 23]
>>>

IMO they can be useful and save you a lil time and space but kinda limited and not something i've really used to much, prob because i don't write many one liners

have fun,
Mark.
Last edited by netytan; September 26th, 2003 at 03:19 AM.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2001
Location
Houston, TX
Posts
383
Rep Power
14
Actually, lambda's are function makers to be more correct. The lambda keyword creates an anonymous function.
Aside from using them with other functional constructs like map and filter, you can use them to return functions from within functions (making your own function-making functions). You can do this with locally nested scopes as well, but sometimes using a lambda is just easier.
5. Well all those explanations were 10000% clearer than any book I'd read. Thanks a bunch, guys.

As of performance, how do lambdas perform versus, say, function calls ?
6. Very welcome Solar! I don't think there's much differance preformace wise, you might find http://www.szgti.bmf.hu/harp/python/fastpython.html interesting though

I'd guess that lambda's would be a lil more efficent since there really just spiced up expressions which get called once then discarded, though they can only be pretty simple.. it really depends on what you want to use them for, but they have nothing on functions IMO

Mark.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2001
Location
Houston, TX
Posts
383
Rep Power
14
Originally posted by SolarBear
As of performance, how do lambdas perform versus, say, function calls ?
It should be identical. As far as I know, lambda-made functions and regularly def-made functions are compositionally identical, it's just that one is bound to a name and one isn't.