+ 17

Lambdas

What’s an example of when a lambda would be better than a normal function? And vice versa. Thanks!

19th Oct 2018, 4:16 AM
DrChicken24
DrChicken24 - avatar
35 Respuestas
+ 35
Good question. I can think of only one situation where I'd recommend using a lambda: when you need a short, single-use function embedded as part of a larger expression. Example: a = [1, 2, 7] b = list(map(lambda x: -x, a)) # so b = [-1, - 2, - 7] OR, if you are insane like me and enjoy making unreadable oneliners. 😅 For every other situation, I think def is the right choice.
19th Oct 2018, 4:50 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 32
Think of lambdas as throwaway functions. Let's say you have a list a = [89,17,69,5,24,48] and you want to filter all elements > 20. You could create a standard function for that: def over20(n): return n > 20 print(list(filter(over20, a))) But you're probably never going to need a function over20() again. So it was kind of pointless to define it in the first place. You can do the same with an anonymous lambda function that is defined in place and only used for this specific task: print(list(filter(lambda x : x > 20, a))) After that, the lambda function is done and can't be accessed anymore because it doesn't even have a name...
19th Oct 2018, 5:22 AM
Anna
Anna - avatar
+ 21
I prefer comprehensions as they seem a bit more readable and are usually shorter. e.g. instead of: print(list(filter(lambda x : x > 20, a))) just use: print([x for x in a if x > 20])
19th Oct 2018, 9:18 AM
David Ashton
David Ashton - avatar
+ 8
I have to agree with Kishalaya and Anna. Single use throw away functions like above will help keep code clean and readable. If the lambda is used more than once, it is recommended to make it a proper function.
19th Oct 2018, 6:22 AM
Steven M
Steven M - avatar
+ 8
Here is the code to test it out https://code.sololearn.com/cK70fD4R6Z4W/?ref=app
19th Oct 2018, 9:38 AM
David Ashton
David Ashton - avatar
+ 8
HonFu Not really. That's a situation where I would use a lambda, e.g. sorting letters alphabetically ignoring case using key=lambda x: x.lower() I would search in Stackoverflow as they post really clever stuff.
19th Oct 2018, 5:06 PM
David Ashton
David Ashton - avatar
+ 6
I'm guilty of spamming lambdas everywhere so I feel qualified to answer :D Basically you'd use them if you need your throwaway function twice. You can define normal functions inside functions, but `foo = lambda x: ...` is an expression so you can define lots of lambdas in little space so that's nice. Here's an extreme example. Say you have an image on a canvas and it's bounding box is [xmin, xmax] * [ymin, ymax] and you want to move or stretch it to be at position [xmin2, xmax2] * [ymin2, ymax2]. You need a function that transforms any coordinates (x, y) to the new (x2, y2). It'd look like this: def stretcher(xmin, xmax, ymin, ymax, xmin2, xmax2, ymin2, ymax2): lerp = lambda min,max,min2,max2: lambda v: (v-min)/(max-min) * (max2-min2) + min2 lerpx = lerp(xmin, xmax, xmin2, xmax2) lerpy = lerp(ymin, ymax, ymin2, ymax2) return lambda x,y: (lerpx(x), lerpy(y)) lerp is "linear interpolation" by the way and it's used a lot (so not something I made up :)
21st Oct 2018, 5:13 PM
Schindlabua
Schindlabua - avatar
+ 6
Schindlabua it looks like you used only one named lambda. And your reason is that it saves space? Okay, I can see that! Though I'd probably still go with "def lerp():" inside strecher. The example looks fairly complicated as it is.
21st Oct 2018, 6:32 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 6
How is a function called 'a' anonymous?
29th Oct 2018, 9:06 AM
Anna
Anna - avatar
+ 5
HonFu old news but totally forgotten! 😆
19th Oct 2018, 6:58 PM
David Ashton
David Ashton - avatar
+ 5
I never understood the purpose of named lambdas. They are even taught in the tutorial and I don't know why 🤔 (and yes, I'm guilty of using them sometimes when I try to write an especially clever poser code 😭)
21st Oct 2018, 10:15 AM
Anna
Anna - avatar
+ 4
David Ashton nice! Using your trick filter and map can usually be replaced by list comprehensions. But sometimes I don't see another (easy) choice. For example, max, min, sort, functools.reduce, itertools.accumulate also take functions as arguments (optional for some of them).
19th Oct 2018, 9:34 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 4
David Ashton, do you have any idea for keyed sorts? 'key=' wants a function, so these statements can get annoyingly long ...
19th Oct 2018, 10:21 AM
HonFu
HonFu - avatar
+ 4
I also like list comprehension more! It is inspired by set builder in math which also gives us the ability to express in our natural language. Beauty of Nature.😃 Just my opinion. I know I am jumping in between talks of legends.😊
19th Oct 2018, 6:06 PM
Roneel
Roneel - avatar
+ 3
I use them if I need a function just once to support an output or something, like: filter(lambda whatever...) or list2.sort(key=lambda whatever).
19th Oct 2018, 8:55 AM
HonFu
HonFu - avatar
+ 3
HonFu a = [[1,2,3],[2,1,3],[2,3,1]] let's say you want to sort by the second element of each sublist: a = sorted(a, key = lambda e: e[1]) -> a will be sorted by each element's (e) 2nd element https://code.sololearn.com/cAe19vAQHEuP/?ref=app
19th Oct 2018, 5:56 PM
Martin Möhle
Martin Möhle - avatar
+ 3
HonFu Ohh ok, thought u asked that 😂 Sorry.. So list comprehension... hmm. Ya probably not possible
19th Oct 2018, 6:09 PM
Martin Möhle
Martin Möhle - avatar
+ 3
I can only think of operator.itemgetter https://code.sololearn.com/cmMnDla8J9LA/?ref=app
19th Oct 2018, 6:16 PM
Anna
Anna - avatar
+ 3
HonFu Here is the code I was referring to. I've been messing around with key= but can't get it to work any other way so far. https://code.sololearn.com/cIr34Ccj9F8C/?ref=app
19th Oct 2018, 6:25 PM
David Ashton
David Ashton - avatar
+ 3
thanks!
19th Oct 2018, 6:29 PM
Roneel
Roneel - avatar