+ 17
Lambdas
What’s an example of when a lambda would be better than a normal function? And vice versa. Thanks!
35 Antworten
+ 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.
+ 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...
+ 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])
+ 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.
+ 8
Here is the code to test it out
https://code.sololearn.com/cK70fD4R6Z4W/?ref=app
+ 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.
+ 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 :)
+ 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.
+ 6
How is a function called 'a' anonymous?
+ 5
HonFu old news but totally forgotten! 😆
+ 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 😭)
+ 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).
+ 4
David Ashton, do you have any idea for keyed sorts? 'key=' wants a function, so these statements can get annoyingly long ...
+ 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.😊
+ 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).
+ 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
+ 3
HonFu Ohh ok, thought u asked that 😂 Sorry.. So list comprehension... hmm. Ya probably not possible
+ 3
I can only think of operator.itemgetter
https://code.sololearn.com/cmMnDla8J9LA/?ref=app
+ 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
+ 3
thanks!