r/learnpython • u/notfromkentohio • Jan 23 '22
I still don't understand the benefit of lambda functions
Hi all,
I've read about lambda functions (for python and other languages), I can WRITE lambda functions just fine, but it seems to me that most places I use them they're just...functions with different syntax. I mean they take args, they return values sometimes. What am I missing here? If anyone has examples of problems they've solved with lambda functions that would also be very helpful.
Thanks!
52
u/m0us3_rat Jan 23 '22
same as list comprehensions. when u need them they really do good work.
filters come to mind. or throw away functions u don't really need to document extensively.
when u need to do some highly predictable small job once.
10
Jan 23 '22
I do feel that lambdas have a bit less utility compared to regular functions if we're drawing a parallel between list comprehensions and for-loops for the purpose.
A lambda function can quickly become un-pythonic in style. It's true that so can comprehensions, but comprehensions are normally built off of earlier parts of code.
Generally my rules are:
(for lambdas) If it takes longer to define a lambda than a function, use a function.
(for comprehensions) If you cannot absolutely use a comprehension that makes sense, write a loop.
But all your other points of throw-away functions for small jobs, I completely agree with.
1
u/m0us3_rat Jan 23 '22
(for lambdas) If it takes longer to define a lambda than a function, use a function.
nowadays i try to stay TDD. and tbh I'm not necessarily good at it .
so my lambdas usually come in the refactoring phase. i only write them directly is when it's obvious.
so my view of them is definitely shifted in that direction.
somebody that writes code first would have a different pov.
also i don't necessarily think it's a bad thing. ppl can express themselves in code however they feel.
I'm not against fancy code if it can read without going crosseyed.
22
u/sme272 Jan 23 '22
They're useful for one off functions that won't be used anywehre else like a list transformation map(lambda x: int(x), my_list). I've also seen them used as the tkinter button action as a way to supply the button function with arguments.
30
u/Yojihito Jan 23 '22
No need for a lambda here.
map(int, my_list)12
Jan 23 '22
Yep, maybe a better example would have a bit more complexity:
map(lambda x: 2*int(x), my_list)4
u/siddsp Jan 23 '22 edited Jan 23 '22
You don't really need a lambda in this case either since you could just do:
(2*int(x) for x in my_list)3
u/jwink3101 Jan 23 '22
You're not wrong about what you need but you also don't need to do a lot of things!
And, of course, your argument would hold for any use of
map!3
u/siddsp Jan 23 '22
Map in some cases can be much better. In this case specifically, using map is slightly faster, but in my opinion less readable because of the visual clutter that the "lambda" keyword and parameter names adds.
If I have multiple iterables that I want to get map from, then it absolutely is necessary, otherwise the generator expression become straight up unreadable.
1
u/AtomicShoelace Jan 23 '22 edited Jan 23 '22
using map is slightly faster
Could you offer some evidence of this? My tests show the generator expression to be faster:
>>> from timeit import timeit >>> from random import randint >>> my_list = [str(randint(1, 1000)) for _ in range(1000)] >>> timeit(lambda: list(map(lambda x: 2*int(x), my_list)), number=10000) 1.457627400000007 >>> timeit(lambda: list((2*int(x) for x in my_list)), number=10000) 1.2212341999999978(here we cast to a list in order to exhaust the map object/generator expression)
In my opinion
mapshould be only used when you already have a defined function you want to map onto the iterable(s). If you have to define a lambda function inline then you should always just use a generator expression (and simplyzipthem together should you want to use multiple iterables).1
u/siddsp Jan 23 '22 edited Jan 23 '22
Could you offer some evidence of this? My tests show the generator expression to be faster:
I ran the tests without creating a list, and this is what I got:
0.003147799987345934 # Map version 0.004475299967452884 # Generator Expression VersionSo it is slightly faster to use map, but that doesn't tell the entire story.
>>> from timeit import timeit >>> from random import randint >>> my_list = [str(randint(1, 1000)) for _ in range(1000)] >>> timeit(lambda: list(map(lambda x: 2int(x), my_list)), number=10000) 1.457627400000007 >>> timeit(lambda: list((2int(x) for x in my_list)), number=10000) 1.2212341999999978(here we cast to a list in order to exhaust the map object/generator expression)
This is not an accurate way to measure the speed, because now you get another aspect that you have to factor in, which is how the compiler is optimized for the list type. Chances are there is a compiler level optimization since the generator expression is nearly identical to a list comprehension.
If we want a better picture of what's going on, we can disassemble the bytecode in both cases to find out what actuall is happening. If we look at the bytecode of map and generator expression, we can see that the generator expression isn't as optimized.
Edit: I put the bytecode on a pastebin because reddit's markdown editor was refusing to cooperate.
1
u/AtomicShoelace Jan 23 '22
Without creating a list? How else are your exhausting the iterators in your tests? It surely wouldn't be a fair comparison if you're just comparing the time it takes to lazily initialize them.
Here is another test that uses a for loop instead of a list constructor to exhaust the iterators:
from timeit import timeit from random import randint my_list = [str(randint(1, 1000)) for _ in range(1000)] def map_test(): for _ in map(lambda x: 2*int(x), my_list): pass def gen_test(): for _ in (2*int(x) for x in my_list): pass print('map:', timeit(map_test, number=10000)) print('gen:', timeit(gen_test, number=10000))which yields roughly the same results as before:
map: 1.7768720000000002 gen: 1.50350679999999981
u/siddsp Jan 24 '22
It's entirely possible. I guess that means that the generator expression is faster and better than a map and lambda?
→ More replies (0)1
u/Ran4 Jan 23 '22
And, of course, your argument would hold for any use of map!
I mean, yeah.
mapandfilterare very rarely needed. List/set/generator comprehensions are often a better idea, and cleaner.1
u/patrickbrianmooney Jan 24 '22
I mean, we could just be writing machine code directly on punch cards. We don't need to be doing anything else.
But there's plenty of reasons why we abstract above that level, and sometimes lambda functions are a useful technique for doing so.
4
2
u/greyduk Jan 23 '22
Tkinter is the only reason I've had to use them
1
u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jan 23 '22
And even there they're not required.
1
u/greyduk Jan 23 '22
Show me how!
For the life of me I couldn't place a button that ran a function without either it running on declaration or it running once and staying in memory (variables not updating) until the program terminates.
1
u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jan 23 '22
Just provide the reference to a regular function:
def on_button_clicked(): print("button clicked") tk.Button(text="Click me!", command=on_button_clicked)1
u/greyduk Jan 24 '22
That doesn't work in my context. It might be worth noting I'm mostly on a closed system running 2.7
2
u/patrickbrianmooney Jan 24 '22
Hard to explain why something doesn't work on someone else's system when they just say "That doesn't work" instead of clearly explaining what goes wrong.
2
u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jan 24 '22
I'm not sure what a closed system is, but this definitely works in Python 2 as well. Tutorials use lambda because they somehow think it makes things easier or clearer, but all it needs is a callable.
1
u/greyduk Jan 24 '22
I wasn't following tutorials... I definitely tried functions first in this case. Both with and without () for good measure. I think it had to do with passing parameters to the function.
9
u/ThePiGuy0 Jan 23 '22
I think it's unlikely that you will find a scenario in which they can be used but a normal function can't. But...they can be a lot cleaner in some situations.
For example, if I have a list of tuples, [(1, 3), (2, 1), (3, 2)], I may want to sort based on the second entry in the tuple. Therefore I need a key function that will take each item in the list and return the value we should use to sort.
Option 1:
def get_key(list_val):
return list_val[1]
my_list = [(1, 3), (2, 1), (3, 2)]
>>> sorted(my_list, key=get_key)
[(2, 1), (3, 2), (1, 3)]Option 2:
my_list = [(1, 3), (2, 1), (3, 2)]
>>> sorted(my_list, key=lambda x: x[1])
[(2, 1), (3, 2), (1, 3)]The other thing to bear in mind is that each function has access to variables that are within the scope they were declared in. For example, if I have some global variables, a function declared as per normal will have access to them.
Similarly, a lambda has access to the variables within the scope that it was declared. The difference being that a lambda can be easily created within a loop, perhaps using the loop variable as a parameter default. The only way to achieve this with a standard function is to declare the function within the loop (very untidy).
2
u/siddsp Jan 23 '22
In this case, you can just use the operator module. It's slightly easier, and the syntax is more readable (in my opinion).
1
u/ThePiGuy0 Jan 23 '22
Interesting module, I often forget that one exists. I guess you were going for the
sorted(my_list, key=operator.itemgetter(1))which is admittedly a cleaner way of doing it.I guess my example would hold up better if the key was the sum of the two values in the tuple (something non-trivial and that hasn't been done by a built in library).
1
u/siddsp Jan 23 '22
I guess my example would hold up better if the key was the sum of the two values in the tuple (something non-trivial and that hasn't been done by a built in library).
The nice part about this is I believe you can use the sum as a key function as well since it takes an iterable, which a tuple is.
my_list = [(1, 3), (2, 1), (3, 2)] sorted(my_list, key=sum)Which should work.
3
u/Rotterdam4119 Jan 23 '22
I’ve noticed a couple times throughout this thread you’ve come up with ways to not use lambda functions. They’ve all made sense. Is there any time that you do use one?
4
u/siddsp Jan 23 '22
I’ve noticed a couple times throughout this thread you’ve come up with ways to not use lambda functions
Yes, that's because I personally don't like typing them lol. I just find it slightly annoying to have to type "lambda" when it adds visual clutter to functions, and the point of a lambda is to not have a name.
Is there any time that you do use one
Yes, I use lambdas when I have a simple function that doesn't make sense naming because it is easy to tell what it's trying to do just by looking at it.
As an example, let's say I have two lists of numbers, and I want to take the difference of their squares (for each). Just by looking at the expression
x**2 - y**2, it's easy to tell what the function is trying to do.In this case, naming a function
difference_of_squaresor anything along those lines doesn't make much sense, and is a little annoying to do.In this case, I "begrudgingly" use a lambda function, because it's the best (and most concise/readable) way to do so. That ends up looking something like this:
a = [2, 3, 1, 4, 9] b = [4, 2, 1, 1, 12] result = map(lambda x, y: x**2 - y**2, a, b)I personally wish Python had nicer syntax for this, but it's better than trying to create a separate function for something this small.
4
u/monkeysknowledge Jan 23 '22
I use it mainly in map/filter functions and occasionally to save a line or two. Like most recently I used to create a function to convert Kelvin to Fahrenheit. No need to take up an extra line of code for that.
3
u/FerricDonkey Jan 23 '22
Two examples of when I use them immediately come to mind:
First, sorting. Suppose you want to sort a list of objects of the class employee by their salary. Then employees.sort(key = lambda e: e.salary)
They key argument has to be a function that you call on the object in the list, and while you could define this function in the normal way, it'd clutter up your namespace and code with dinky functions that don't get used much.
Second is for guis. Say you're making a board game game. You might have a general function for buying resources with money or whatever. But you might decide that you want to have a different button to buy each resource. You can define those buttons in a loop, with the callback function being something like
for resource in RESOURCES:
button = ButtonClass(<blah>, callback = (lambda r = resource: self.buy_resource(r))With, of course, stuff specific to whatever gui framework you're using. (Though I've heard that people also use functools.partial or something for this sort of thing, I've learned this method first so tend to use it.)
Similar cases pop up a lot - you need a disposable function you only need it in one place. Lambdas are great for that.
5
Jan 23 '22
I frequently use lambda for sorting:
>>> from dataclasses import dataclass
>>> @dataclass
... class Person:
... height: float
...
>>> sorted([Person(150), Person(160), Person(170)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'Person' and 'Person'
>>> sorted([Person(150), Person(160), Person(170)], key=lambda p: p.height)
[Person(height=150), Person(height=160), Person(height=170)]
>>> sorted([Person(150), Person(160), Person(170)], key=lambda p: -p.height)
[Person(height=170), Person(height=160), Person(height=150)]4
u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jan 23 '22
I prefer
attrgetterin such case.from operator import attrgetter sorted(list_of_people, key=attrgetter("height"))3
u/siddsp Jan 23 '22
Forget prefer, this is what attrgetter is for!
0
Mar 26 '22
[deleted]
1
u/siddsp Mar 26 '22
That's an issue with IDEs right now, and not attrgetter itself. It isn't hard to check if you've done anything wrong.
0
Mar 27 '22
[deleted]
1
u/siddsp Mar 27 '22
It's easily fixable. It's called not being careless.
0
4
Jan 23 '22
People rant and rave about them but to me they are an anti-pattern as they make code less readable. I would never use them.
0
Jan 23 '22
[deleted]
2
u/happymellon Jan 24 '22
I bet they then go on to use JavaScript and create abominations where you get stack traces without any hint as to where it could actually be failing too.
2
Jan 23 '22
difference lies in scope. if your function scope is entire project than use normal function and if it is in limited local code block area then go for lamda funciton no need to create a seperate function
2
u/m1cr0_c0de Jan 23 '22
Lambda is just an anonymous function, it is useful when you need to pass a function around. Another example is passing a function as a predicate to another (object/function) to configure its behavior. This little thing that you do not need to name all the things around is sometimes very useful.
2
Jan 23 '22
I'm not sure there's anything they can do that a normal function couldn't. They're handy in places. But frankly, after a few decades at writing software I just stay the hell away from them unless I'm using something like lisp.
They're an absolute bitch to test and, including them in-place makes things pretty brittle.
1
u/darkangelstorm Apr 02 '25
They are useful in high level languages, not so much in lower languages. They have these main trait that make people flock to them:
You can pass them around between functions, classes, etc
You can store them, so you can do more of #1
You can use them to simplify complex functions (make them more readable) while making #2 and #1 easier.
Operations that were once avoided can now be written easily without having to copy or paste anything, though this brings us to threshold point of the cons...
The cons being:
Possible unintended hiding of critical points of execution
Pro #3 might end up being a con, because readability always sacrifices visibility, whether you want it or not.
Like con #2, pro #3 might end up being the reverse -- more complex than more simplified. I've seen this happen time and again. Production developers just love to run endurance tests on a maintenance programmer's sanity.
Though the benefits are great, reusability suffers because likely these gems are largely have a single point of actual usage, which means it will have to be reinvented. Or worse, it is intended to be used everywhere from that point on introducing more of #3.
They are the fuel that keeps algorithms growing. They are largely misunderstood or overunderstood. Be careful when making them and only include them in your user's scope when you absolutely have to. Nobody enjoys having to hit the books because every.single.time(tm) a new magic black rubix cube has been invented and you must learn it whether you like it or not because mister lead dev says so.
1
u/Reedemer0fSouls Oct 23 '25 edited Oct 23 '25
Apologies for resurrecting this thread, but the core explanation is very simple. So let me rephrase the question: what is the benefit of using lambda functions over mere expressions? Well, the answer is that sometimes functions (or other ... stuff) do not take expressions as arguments, but functions, so lambda has been invented as a way to quickly masquerade an expression as a function in order to fulfill type requirements (... without going to the trouble of designing a proper function for what should be just an expression).
It's that simple. All other considerations, such as speed etc. are secondary and tangential/unintended.
1
u/tatted_DE Jan 14 '26
I absolutely love them for my math programs, say as easy input to a line or a vector field function
1
u/NorthWestApple Sep 22 '26
A lot of the stuff added to languages like C++ since 1998 are just BS. There is absolutely nothing about C++ that lets me do anything I can't already do in C. The only addition I use is function overloading. Classes and namespaces never, unless a piece of code I have needed to import requires it (again, never).
Most of this modern language junk seems to exist just so someone could get a PhD.
1
u/vardonir Jan 23 '22
most basic use at the top of my head is the key in sorted
i use them a lot in pandas df.apply()operations, mostly encoding simple but annoying columns, so i'd write something like 1 if 't' else 0
they also come up a lot in connecting functions that take arguments to pyqt signals, but that's pretty advanced
1
u/tagapagtuos Jan 23 '22
Not sure if it's just a coincidence or by design but I don't usually see Python functions using callbacks (compared to languages like JS).
This code below:
def compute(func, op1, op2):
return func(op1, op 2)
compute(lambda x, y: x+y, 1, 2) # 3
compute(lambda x, y: x-y, 4, 2) # 2Not the most ideal design but passing a function within a function and calling it inside is one of the ideal use case.
1
u/siddsp Jan 23 '22 edited Jan 23 '22
You don't need to do this for normal operators like adding and subtracting. Instead, you can just use the operator module.
1
Jan 23 '22
This seems like something that could be refactored as a decorator.
2
u/tagapagtuos Jan 23 '22
Decorators, indeed, take another function as an argument. Only difference being, decorators return functions themselves, not the result of a function call.
Callback functions on the other hand, are mostly used to reduce coupling.
For example,
pd.read_excel('file.xlsb', engine='pyxlsb') # (not-so-actual) behind the scenes: # dynamically import pyxlsb # engine.read(file_name)In this code example, the code does not have a long if-else conditionals just so it can determine which engine to use. Instead, the responsibility to specify the engine was given to the user, and the function just has to call
engine.read().Another example of callback I've seen in the wild is this one.
win32gui.EnumWindowstake a callback as its argument.Again, I don't see this pattern a lot on Python code. Even in my example, I only have to pass a string in
enginebecause importlib exists. Andwin32guiis actually just a wrapper for Windows API.
1
Jan 23 '22
You’re not missing anything… a lambda is an anonymous function, literally exactly the same thing as a regular function, just without a name. If anything they’re more restrictive in Python, since compound statements (ie if/elif/else, while, try/except, etc) can’t be used within them, as they’re only a single expression.
However that last bit kind of points at their value… there are a lot of circumstances where you need to pass a function that’s just a single line long and will never be re-used, so why bother with a full named function that takes up memory? If I reach for lambda it’s usually in one of these places, as a function that I’m passing into map, filter, sorted, or re.sub, a function which has no life or utility except in the act and context of the operation I’m using it to perform.
Could I use a named function in every such case? Sure… but why?
1
u/JoeDeluxe Jan 23 '22
Why would you need to pass a function , wouldn't you just call it?
1
Jan 24 '22
Functions like filter and map and the key parameter in sorted take a function you pass in and apply it to every item in an Iterable. Similarly the second parameter of re.sub can be passed a callback function that will be called with each successive pattern match. There’s plenty of here for first class functions.
1
u/eyadams Jan 23 '22
They improve readability when you need a one-off function. I find myself using them all the time in web scraping projects, when I want to find tags that match multiple criteria:
# soup is defined elsewhere
anchors = soup.find_all(lambda tag: True if (tag.name == 'a'
and 'href' in tag.attrs
and 'https://link/to/web/site/' in tag['href'])
else False)I could define the lambda in a separate function, but having the lambda right there, in the code, instead of somewhere else in my code base, is really nice.
1
u/TheRNGuy Jan 23 '22
There are few times you have to use them, like key in sorting list of tuples when key is not 0th item of tuple.
I also had one timein Qt when not using lamba will not fire callback when you move slider.
1
1
u/MoonParkSong Jan 23 '22
The way I used them is to used them inside a function that is when I don't want to create another function.
for example: func(map(lambda item: func2(item.start, item.end), [item list comprehension]))
1
Jan 23 '22
A lot people have defined them well, but one poster mentioned how they are "anonymous functions". I wanted add one point in comparison with javascript.
I am not JS fluent, but I have done some Mozilla tutorials. From what I have seen, some JS coders use anonymous functions within other defined functions for flexibility and execution. It's not the case in Python, and it's almost discouraged except for specific applications, so they do seem a little random...kinda like an appendix in a human.
1
u/SpicyVibration Jan 23 '22
I needed one the other day to create a default factory from a structure (a list) that wasn't callable.
1
Jan 23 '22
I haven't gotten to study legit functional programming yet but I'm pretty sure they exist in the language to accommodate that paradigm. Someone feel free to add to and/or correct this statement.
1
u/_Yn0t_ Jan 24 '22
One illustration of its utility I saw recently is that in some GUIs, if you implement a function on press of a button, and you need to pass on parameters, a lambda function will add an extra step that prevents the function to break your code. I still need to dig into this a bit more but it was very convinient.
1
u/d3v3ndra Jan 24 '22
They are awesome tech, init and handlers function strategies are very cost effective.
1
u/pablo8itall Jan 24 '22
The are for readablity and are also faster. If you have a couple of one-of sorts or calculations they are perfect.
1
u/khldooon Jan 24 '22
Whene you are coding, you sometimes need a function that you will call only one time, this is the best case to implement a lambda function instead of using def syntax
1
u/winginglifelikeaboss Jan 24 '22
The world before lambda functions did just fine, it's just an easier anonymous one line function.
1
u/EmmaGao8 Programmer | 👩🏻💻👩🏻🔬👩🏻🚀 Jan 24 '22
Lambda is a convient and short way to write small functions.
1
u/martnym Jan 24 '22
I've found them very useful for creating one-off callback functions when using modules like tkinter which frequently requires them (callbacks). In the same context, they can be used to change what arguments are passed to the callback, if defaults are insufficient (or non-existent).
1
1
u/mohsinnaqvipro Jul 01 '22
Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.
1
u/arcticblue Jan 23 '22
They are a useful feature for anal code reviewers to hold up merge requests as a flex. "This could be a lambda function here to make this code 2 lines shorter". I hate it when I have a merge request held up for no good reason other than the person reviewing wants to play code golf and end up making me miss deadlines as a result.
0
Jan 23 '22
[deleted]
5
u/notfromkentohio Jan 23 '22
It did help, thank you. The difference in overhead is interesting, I hadn’t considered that
3
u/FancyASlurpie Jan 24 '22
Yeh I mean if your using lambdas because a function definition has too much overhead you should probably be just writing in a lower level language.
1
u/Kfct Jan 24 '22
How do you mean lower level language? Like one of the C's?
1
u/FancyASlurpie Jan 24 '22
Yeh potentially c, c++, rust all have pretty good support for integrating with python.
378
u/Spataner Jan 23 '22 edited Jan 23 '22
That's exactly what they are. The main syntactical difference between the two is that a regular function definition is a statement, whereas a lambda function definition is an expression. Lambda functions can therefore be defined and then passed to another function in one step, without having to go through a local name (that's also why lambda functions are sometimes called anonymous functions, they don't necessarily have a particular name). Lambda is a convenience feature to more easily define small, one-off functions.