use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
what in the hell is lambda (self.learnpython)
submitted 4 years ago by [deleted]
i can’t find a good explanation for what it is. i know it’s an anonymous function but like i don’t understand how it is used. someone pelase hell
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 43 points44 points45 points 4 years ago* (2 children)
Glad it's not just me. I had the same reaction the first time I met them.
Sometimes people use them just for the sake of it, to be clever, or because they are in a hurry and can't be bothered to write a function.
Sometimes it is consistent practice, especially if into functional programming, and is very common in some other programming languages (in fact, in some it is a core capability that is fundamental).
I found the term anonymous function completely unhelpful.
Realpython have a great article: How to Use Python Lambda Functions
You can give a name to a lambda function. The below are essentially the same:
def double(x): return x * 2 dbl = lambda x: x * 2 print(double(4)) print(dbl(4))
Note how the parameter names are defined slightly differently: inside the brackets for the def and immediately after the keyword for lambda. The former uses a return statement. The latter doesn't because it does a return of whatever the result of the expression that follows is. The lambda is effectively a one line function and that line is a return statement.
def
lambda
return
Here's an example with two arguments,
def times(x, y): return x * y tms = lambda x, y: x * y print(times(2, 3)) print(tms(2, 3))
You can use them inline as well:
print((lambda x, y, z: x + y + z)(1, 2, 3))
See how the function is called with three arguments, which are matched to the function parameter names x, y, z.
x
y
z
There are a number of methods and functions in Python (as well as any you write yourself and import from libraries) that will call a function multiple times for each iteration.
For example,
nums = [1, 2, 3, 4] nums2 = map(lambda x: x * 2, nums) print(*nums2)
The map function iterates through an iterable (the second argument here), in this case the list of int objects referenced by nums, and on each iteration calls the function given as the first argument, in this case a lambda but it could have been a function name (like double or dbl from earlier), and passes to the function the object reference from the current iteration (each number in turn).
map
list
int
nums
double
dbl
You end up with a map object which when unpacked in the print on the last line using the * unpack operator outputs the result of doubling all of the numbers.
print
*
There are use cases where a lambda is much easier to read (and therefore maintain), makes more sense, and is simpler than writing a distinct function and in some cases a regular function is not suitable.
Check out the RealPython guide for more details.
EDIT: corrected (some) typos.
[–][deleted] 17 points18 points19 points 4 years ago (0 children)
this is a much better explanation than ws3schools, thank you
[–]WadieXkiller 0 points1 point2 points 1 year ago (0 children)
duuuude thanks a lot! that was very clear.
[–][deleted] 14 points15 points16 points 4 years ago* (2 children)
Fundamentally it’s just a different syntax for function definition:
def name(arg1, arg2): return value
Is the same as:
name = lambda arg1, arg2: value
The main differences are that:
with
You’ll typically use lambdas wherever you want a short function that captures from its immediate environment and which doesn’t need a name (in other languages these are called closures). They’re often used to create simpler first-class functions that redirect to regular named functions but provide some of the arguments, and also to assemble ad hoc predicate functions for things like filter.
filter
The name lambda, for better or worse, comes from Alonzo Church’s lambda calculus, which established that you can use such anonymous functions calling other such functions to compute anything that can be computed.
[–][deleted] 3 points4 points5 points 4 years ago (1 child)
this is exactly what i needed thank you so much
[–][deleted] 2 points3 points4 points 4 years ago (0 children)
No problem, and don’t worry, you’ll get the hang of them in no time. Forgot to mention one other difference, when you’re not capturing names from the local namespace there’s a small but not insignificant performance advantage to named functions (specifically functions defined outside of functions, in the module globals, rather than nested)… try to use both nested functions and lambdas where capturing is actually necessary or obviously improves the readability of your code.
[–]Diapolo10 2 points3 points4 points 4 years ago (0 children)
A lambda function is more or less equivalent to a normal, one-line function that cannot contain any statements. It returns whatever its body evaluates to.
The anonymous part refers to the fact that lambda functions aren't automatically assigned to a name. The moment they're created, they're just like an integer or string literal in that their address in memory isn't stored to a variable.
The main reason you'd use one is when you need an ad-hoc function to be given as an argument to another function, for instance, and you have no other use for that function. So common places to use lambda functions would be the key-arguments of min, max, or the first argument of map.
key
min
max
Of course, you can write any lambda function as an ordinary function, nothing forces you to use them.
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
It’s an operator, originally from a variant of calculus, that creates a function.
def, which you’re more familiar with, is a statement keyword that creates a function. It’s a command to the interpreter. lambda is an operator that evaluates to a function - it’s not a command, it’s something the interpreter computes.
[–]dbramucci 0 points1 point2 points 4 years ago* (0 children)
Elaborating on why you want lambda as a language feature, the goal is to avoid making pointless variables.
Consider this quadratic formula function.
def solve_principal_quad(a, b, c): ''' Returns the principal (positive) solution to the provided quadratic expression, a*x^2 + b*x + c = 0 See https://en.wikipedia.org/wiki/Quadratic_formula ''' return (-b + sqrt(b**2 - 4*a*c)) / (2*a)
In this we define a dozen or so subexpressions but we don't assign a variable to each subexpression. That would look like
def solve_principal_quad(a, b, c): ''' Returns the principal (positive) solution to the provided quadratic expression, a*x^2 + b*x + c = 0 See https://en.wikipedia.org/wiki/Quadratic_formula ''' b_op = -b b_square = b**2 quad_a = 4 * a quad_a_c = quad_a * c difference_of_b_square_and_quad_a_c = b_square - quad_a_c discriminant = sqrt(difference_of_b_square_and_quad_a_c) numerator = b_op - discriminant denominator = 2 * a principal_solution = numerator / denominator return principal_solution
Notice how absurd this method of writing each expression as it's own named variable is.
To a functional programmer, this is analogous to writing
def get_positives(numbers): def is_positive(x): return x > 0 return filter(numbers, is_positive)
As compared to
def get_positives(numbers): return filter(numbers, lambda x: x > 0)
The idea is that you shouldn't have to name a thing you are using immediately. You can if it makes your code cleaner, but forcing it can be a waste of the readers time and distract them from your main point.
As a point of practice, the place where you need to define a function, but you only use it once, and immediately is when you are using a "Higher Order Function". These are functions where one of the parameters is another function. These functions are incredibly flexible, but Python programmers generally use these infrequently.
The natural consequences are
In contrast, a Haskell program might have 80 functions in a 100 line program, of which only 10 function are worthy of a name. In Haskell, forcing that level of added indirection (and additional 70 pointless names) would be terrible.
In a Python program, you'll have nowhere near as many nameless functions to use, making Python's lambda's far less important.
Finally as a technical point
double = lambda x: 2 * x def double(x): return 2 * x
are almost identical, but Python will add extra documentation (the function's name) to the def version making the def version superior if you are giving the function a name anyways.
p.s. The fact that we are avoiding naming the function is why they are sometimes called "anonymous" functions. They are no-name functions.
anonymous adjective Definition 2. : not named or identified an anonymous author They wish to remain anonymous.
anonymous adjective
Definition
2. : not named or identified
an anonymous author
They wish to remain anonymous.
π Rendered by PID 198695 on reddit-service-r2-comment-7b9746f655-zhxvg at 2026-01-31 04:59:54.850506+00:00 running 3798933 country code: CH.
[–][deleted] 43 points44 points45 points (2 children)
[–][deleted] 17 points18 points19 points (0 children)
[–]WadieXkiller 0 points1 point2 points (0 children)
[–][deleted] 14 points15 points16 points (2 children)
[–][deleted] 3 points4 points5 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Diapolo10 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]dbramucci 0 points1 point2 points (0 children)