Welcome, @GeriReshef ! Good question. The basic distinction between creating a function with def and a lambda expression, besides syntactic sugar, is that lambda does so without assigning to a name; in fact, the other common name for a lambda is an anonymous function. If you’re going to create a la… Answer from CAM-Gerlach on discuss.python.org
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python Examples Python Compiler ... anonymous function. A lambda function can take any number of arguments, but can only have one expression....
🌐
Python.org
discuss.python.org › python help
What is the purpose of Lambda expressions? - Python Help - Discussions on Python.org
December 8, 2021 - This is a typical example for beginners: x=lambda n:2*n print(x(7)) Otherwise I would create a function: def dbl(n): return 2*n print(dbl(7)) Of course: I can write simply 2*7, but the idea is to save a complex formula in an object once, and ...
Discussions

What is the point of lambda in Python?
lambdas are callables, or in other words, functions. Specifically, they are anonymous functions. A better name for lambda would have been make_function lambda x: x + 1 is equivalent to def add_one(x): return x + 1 The major difference is that the lambda does not need to occupy a namespace, like the definition of add_one This is useful for inlining simple functions. If you were to assign the lambda to a name, it is essentially exactly the same thing as defining the function as we did before. >>> add_one = lambda x: x + 1 >>> add_one(1) 2 Note that the above is considered an anti-pattern in Python and, by assigning it a name, pretty much defeats the purpose of lambda to begin with. You might see lambdas used for inlining definitions, such as in the creation of a dictionary whose values hold callables (functions) operations = { 'DELETE': lambda dynamo, x: dynamo.delete_item(**x), 'GET': lambda dynamo, x: dynamo.scan(**x), 'POST': lambda dynamo, x: dynamo.put_item(**x), 'PUT': lambda dynamo, x: dynamo.update_item(**x), } The alternative to this would be to do def delete(x): return dynamo.delete_item(**x) def get(x): return dynamo.scan(**x) def post(x): return dynamo.put_item(**x) def put(x): return dynamo.update_item(**x) operations = { 'DELETE': delete, 'GET': get, 'POST': post, 'PUT': put, } You can see the lambda significantly reduced the amount of code to express the same idea, and did not occupy name spaces for get delete post and put -- That's the benefit of using lambda More on reddit.com
🌐 r/learnpython
21
52
February 27, 2017
python - How are lambdas useful? - Stack Overflow
Lambda expressions are becoming popular in other languages (like C#) as well. They're not going anywhere. Reading up on closures would be a useful exercise to understand Lambdas. Closures make a lot of coding magic possible in frameworks like jQuery. 2009-05-20T21:36:17.923Z+00:00 ... Don't confuse lambdas and first-class functions. Python ... More on stackoverflow.com
🌐 stackoverflow.com
can someone explain lambda to a beginner?
It's a function that has no name, can only contain one expression, and automatically returns the result of that expression. Here's a function named "double": def double(n): return 2 * n print(double(2)) results in: 4 You can do the same thing without first defining a named function by using a lambda instead - it's creating a function right as you use it: print((lambda n: 2 * n)(2)) You can pass functions into other functions. The map function applies some function to each value of a sequence: list(map(double, [1, 2, 3])) results in: [2, 4, 6] You can do exactly the same thing without having defined double() separately: list(map(lambda n: 2 * n, [1, 2, 3])) More on reddit.com
🌐 r/learnpython
46
102
October 10, 2024
Lambda function
map applies a function to every element of an iterable. Sometimes these functions are very simple operations; for example, if I wanted to double every number in a list. It's wasteful to define an entire new function (with the def keyword) just for this one simple operation. Lambda creates a callable function that we can use. Here's an example of how we could create that and use it with map. numbers = [1, 5, 20, 50] map(lambda x:x * 2, numbers) More on reddit.com
🌐 r/learnpython
23
13
April 1, 2024
🌐
Reddit
reddit.com › r/learnpython › lambda function
r/learnpython on Reddit: Lambda function
April 1, 2024 -

I understand what the lambda function is, its an anonymous function in one line, however why using it, and what really is it? I mean every code I looked at, has it and don't forget map() reduce and filter() function are used with it, what are all these used for and why, I did my research but I still don't understand, (I have a baby's brain 🧠 y'all)

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Argument (x): input value passed to the lambda function. Expression (x**2): operation performed on the argument and returned as the result.
Published: May 18, 2026
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › what's all the fuss about `lambda` functions in python
What's All the Fuss About `lambda` Functions in Python
February 15, 2025 - You can define them with one or more parameters (or none). However, you can only have a single expression that evaluates to a value. This is the value returned by the lambda function. Therefore, you can't have complex algorithms in a lambda function, just whatever you can fit in a single expression!
🌐
AppSignal
blog.appsignal.com › home › python › how to use lambda functions in python
How to Use Lambda Functions in Python | AppSignal Blog
October 16, 2024 - Limitations in error handling and debugging: As Lambda functions can only contain a single expression, they can't include statements, like the try-except block for error handling.
Find elsewhere
🌐
Python Basics
python-basics-tutorial.readthedocs.io › en › latest › functions › lambda.html
Lambda functions - Python Basics
It is a small and restricted function that is no longer than one line. Like a normal function, a lambda function can have several arguments, but only one expression that is evaluated and returned.
🌐
Codecademy
codecademy.com › article › python-lambda-function
Python Lambda Functions Explained (With Examples) | Codecademy
Lambda programming in Python is useful when a function is passed as an argument or when you need a simple operation. The syntax of Python’s lambda function is as follows: ... expression: This is what gets evaluated and returned.
🌐
Medium
medium.com › datatau › what-they-are-and-how-to-use-lambda-expressions-in-python-41aa87852c83
What they are and how to use Lambda expressions in Python | by Borja Uría | DataTau | Medium
May 14, 2020 - Unlike a normal function, we do not define it with the standard keyword def that we use in Python. Instead, Lambda functions are defined as a line that executes a single expression.
🌐
freeCodeCamp
freecodecamp.org › news › python-lambda-function-explained
How the Python Lambda Function Works – Explained with Examples
December 17, 2024 - Lambda functions are efficient whenever you want to create a function that will only contain simple expressions – that is, expressions that are usually a single line of a statement.
🌐
Python Morsels
pythonmorsels.com › lambda-expressions
Python's lambda functions - Python Morsels
October 25, 2023 - So lambda expressions allow us to write shorter code by defining a function on the same line that we pass that function to sorted. Lambda functions are anonymous functions, meaning they have no name.
🌐
Nanyang Technological University
libguides.ntu.edu.sg › python › lambdaexpressions
2.8 Lambda Expressions - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
August 26, 2026 - Learn practical Python programming skills for basic data manipulation and analysis. ... Lambda expressions does what a function does without the need to properly define the function using def. The body of a lambda expression will be similar to what we will put into the return of a function ...
🌐
Trey Hunner
treyhunner.com › 2018 › 09 › stop-writing-lambda-expressions
Overusing lambda expressions in Python
September 27, 2018 - I’m going to explain how I see lambda expressions and why I tend to recommend my students avoid using them. Lambda expressions a special syntax in Python for creating anonymous functions.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-map-lambda.html
Python Map Lambda
A lambda expression is a way of creating a little function inline, without all the syntax of a def.
🌐
DigitalOcean
digitalocean.com › community › tutorials › lambda-expression-python
Python Lambda Expressions Explained with Examples | DigitalOcean
Master Python lambda expressions with this step-by-step guide. Learn syntax, use cases, and common mistakes for writing cleaner functional code.
🌐
W3Schools
w3schools.com › cpp › cpp_functions_lambda.asp
C++ Lambda Functions
A lambda function is a small, anonymous function you can write directly in your code.
🌐
Reddit
reddit.com › r/learnpython › what is the point of lambda in python?
r/learnpython on Reddit: What is the point of lambda in Python?
February 27, 2017 -

I was wondering what was the use of this feature, so I googled it and found a StackOverflow post where a user shows several examples of how the lambda function could be used. However, all of these seems to be easily replaceable by normal variable attributions, e.g.:

lambda x: x**2 + 2*x - 5

could be replaced by

x = x**2 + 2*x-5

So why do we use lambda and when?

Top answer
1 of 5
48
lambdas are callables, or in other words, functions. Specifically, they are anonymous functions. A better name for lambda would have been make_function lambda x: x + 1 is equivalent to def add_one(x): return x + 1 The major difference is that the lambda does not need to occupy a namespace, like the definition of add_one This is useful for inlining simple functions. If you were to assign the lambda to a name, it is essentially exactly the same thing as defining the function as we did before. >>> add_one = lambda x: x + 1 >>> add_one(1) 2 Note that the above is considered an anti-pattern in Python and, by assigning it a name, pretty much defeats the purpose of lambda to begin with. You might see lambdas used for inlining definitions, such as in the creation of a dictionary whose values hold callables (functions) operations = { 'DELETE': lambda dynamo, x: dynamo.delete_item(**x), 'GET': lambda dynamo, x: dynamo.scan(**x), 'POST': lambda dynamo, x: dynamo.put_item(**x), 'PUT': lambda dynamo, x: dynamo.update_item(**x), } The alternative to this would be to do def delete(x): return dynamo.delete_item(**x) def get(x): return dynamo.scan(**x) def post(x): return dynamo.put_item(**x) def put(x): return dynamo.update_item(**x) operations = { 'DELETE': delete, 'GET': get, 'POST': post, 'PUT': put, } You can see the lambda significantly reduced the amount of code to express the same idea, and did not occupy name spaces for get delete post and put -- That's the benefit of using lambda
2 of 5
9
lambda defines a function (not a variable). It's exactly the same as def, except we can use it in a single line. It's syntactic sugar ... it's only use is to make code shorter and neater. So instead of this: def small_func(x): return x**2 print(list(map(small_func, range(10)))) # prints all the squares from 0 - 9 We can do this: print(list(map(lambda x: x**2, range(10)))) # prints all the squares from 0 - 9 As a beginner, you are welcome to forget it exists.
🌐
W3Schools
w3schools.com › python › gloss_python_lambda.asp
Python Lambda Function
Python Examples Python Compiler ... anonymous function. A lambda function can take any number of arguments, but can only have one expression....
Top answer
1 of 16
1073

Are you talking about lambda expressions? Like

lambda x: x**2 + 2*x - 5

Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff. Example:

mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])

sets mult3 to [3, 6, 9], those elements of the original list that are multiples of 3. This is shorter (and, one could argue, clearer) than

def filterfunc(x):
    return x % 3 == 0
mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9])

Of course, in this particular case, you could do the same thing as a list comprehension:

mult3 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0]

(or even as range(3,10,3)), but there are many other, more sophisticated use cases where you can't use a list comprehension and a lambda function may be the shortest way to write something out.

  • Returning a function from another function

      >>> def transform(n):
      ...     return lambda x: x + n
      ...
      >>> f = transform(3)
      >>> f(4)
      7
    

    This is often used to create function wrappers, such as Python's decorators.

  • Combining elements of an iterable sequence with reduce()

      >>> reduce(lambda a, b: '{}, {}'.format(a, b), [1, 2, 3, 4, 5, 6, 7, 8, 9])
      '1, 2, 3, 4, 5, 6, 7, 8, 9'
    
  • Sorting by an alternate key

      >>> sorted([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs(5-x))
      [5, 4, 6, 3, 7, 2, 8, 1, 9]
    

I use lambda functions on a regular basis. It took me a while to get used to them, but eventually I came to understand that they're a very valuable part of the language.

2 of 16
411

lambda is just a fancy way of saying function. Other than its name, there is nothing obscure, intimidating or cryptic about it. When you read the following line, replace lambda by function in your mind:

>>> f = lambda x: x + 1
>>> f(3)
4

It just defines a function of x. Some other languages, like R, say it explicitly:

> f = function(x) { x + 1 }
> f(3)
4

You see? It's one of the most natural things to do in programming.

🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
The first form means that the name of the resulting function object is specifically ‘f’ instead of the generic ‘<lambda>’. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e.