A Python lambda function is a small, anonymous function defined using the lambda keyword. It can take any number of arguments but is limited to a single expression, which is automatically returned. The syntax is lambda arguments: expression.

Key Features

  • Anonymous: Does not require a name unless assigned to a variable.

  • Single Expression: Only one expression is allowed; no return keyword needed.

  • Inline Use: Ideal for short, one-time operations, especially as arguments in higher-order functions like map(), filter(), and sorted().

Example Usage

# Square a number
square = lambda x: x ** 2
print(square(5))  # Output: 25

# Double numbers in a list using map()
numbers = [1, 2, 3]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled)  # Output: [2, 4, 6]

# Filter even numbers
evens = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4]))
print(evens)  # Output: [2, 4]

When to Use

  • Use lambda for simple, temporary functions where defining a full function with def would be unnecessary.

  • Use regular functions (def) for reusable, complex logic, or when you need multiple statements, loops, or docstrings.

Limitations

  • Cannot include try-except, loops, or multiple statements.

  • Can become hard to read if the expression is complex.

Note: While lambda functions enhance code conciseness in functional programming, they should be used judiciously to maintain readability.

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])) Answer from ssnoyes on reddit.com
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... A lambda function is a small anonymous function....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Lambda keyword (lambda): Defines an anonymous (inline) function in Python.
Published   5 days ago
Discussions

What is the purpose of Lambda expressions?
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 reuse it several times. More on discuss.python.org
🌐 discuss.python.org
1
December 8, 2021
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
24
11
September 15, 2023
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
99
October 10, 2024
python - Lambda function in list comprehensions - Stack Overflow
0 Why is this simple lambda function giving bizarre outputs? 0 Using Lambda inside a loop : how to get the current loop variable value inside the lambda? 1 pySpark not picking variable from for loop correctly · 6 Python: Creating a dictionary using list comprehension from a list using lambda More on stackoverflow.com
🌐 stackoverflow.com
🌐
The Python Coding Stack
thepythoncodingstack.com › p › whats-all-the-fuss-about-python-lambda-functions
What's All the Fuss About `lambda` Functions in Python
December 1, 2023 - But if you feel the need to name ... to do so: And that's all there is, really. Python's lambda functions are just functions with no name....
🌐
AppSignal
blog.appsignal.com › 2024 › 10 › 16 › how-to-use-lambda-functions-in-python.html
How to Use Lambda Functions in Python | AppSignal Blog
October 16, 2024 - We've created an anonymous function that takes two arguments, x and y. Unlike traditional functions, Lambda functions don't have a name: that's why we say they are "anonymous." Also, we don't use the return statement, as we do in regular Python functions.
🌐
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 reuse it several times.
🌐
Reddit
reddit.com › r/learnpython › lambda function
r/learnpython on Reddit: Lambda function
September 15, 2023 -

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)

🌐
docs.python.org
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope: >>> def make_incrementor(n): ...
Find elsewhere
🌐
Codecademy
codecademy.com › article › python-lambda-function
Python Lambda Functions Explained (With Examples) | Codecademy
Dive deeper into the unique ways to utilize functions to create cleaner and more efficient software. ... A Python lambda function is an anonymous function that lets you write quick, inline functions without using the def keyword.
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python
Building Lambda functions with Python - AWS Lambda
Runtime: Choose Python 3.14. Choose Create function. The console creates a Lambda function with a single source file named lambda_function. You can edit this file and add more files in the built-in code editor. In the DEPLOY section, choose Deploy to update your function's code.
🌐
Reddit
reddit.com › r/learnpython › can someone explain lambda to a beginner?
r/learnpython on Reddit: can someone explain lambda to a beginner?
October 10, 2024 -

I am a beginner and I do not understand what lambda means. Can explain to me in a simple way?

Top answer
1 of 16
99
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]))
2 of 16
33
Before we delve into the topic, I wanted to make something clear about lambdas: YOU DO NOT EVER HAVE TO USE THEM! Lambdas are more of an intermediate Python topic and they don't have any inherent functionality that you can't do with a standard function. If you are having trouble reasoning about them, don't worry about it, and just use regular functions instead. Python allows you to do everything you'd want for a lambda with regular functions, it's just a matter of how concise and readable something might be. With all that being said, lambdas are anonymous functions. This means the function has no "name" and is instead assigned to a value or variable. For example, this is a "normal" function: def foo(a, b): return a + b In this case, you've defined a function called foo that takes two parameters and returns those parameters added together. Pretty standard. A lambda, on the other hand, is not defined on program start: foo = lambda a, b: a + b These are 100% equivalent: you can call them using the same syntax. So why would you ever use a lambda? In Python, a function can be used anywhere a lambda could be, but lambdas are often used when you want the definition of the function to be in line with its use. Lambdas tend to be used when you want to use functional programming design in Python (or other languages that support them), as you can "chain" these types of functions to create complex behavior that makes sense in a simple way when reading it. Where this really comes in handy is when you want to do things like sort or filter list data. For example, let's say you have a list of numbers, and want to only get the numbers over 100. You could write a function: my_list = [10, 150, 75, 100, 450, -20] def over_one_hundred(lst): new_lst = [] for num in lst: if num >= 100: new_lst.append(num) return new_lst print(over_one_hundred(my_list)) # Output [150, 100, 450] This works, but is a lot of code for something fairly common and simple. A list comprehension also works in this case: def over_one_hundred(lst): return [l for l in lst if l >= 100] print(over_one_hundred(my_list)) Much more compact, but still requires either a function or a fairly verbose list comprehension. And without a comment, it's not necessarily obvious at a glance the purpose of this list comprehension. It also only works on lists What if we instead use Python's filter function? This takes a sequence, which includes lists, but also includes dictionaries or other similar structures, plus a function that determines what is used for filtering. This is a perfect place for a lambda: over_one_hundred = list(filter(my_list, lambda x: x >= 100)) The list portion here is important, because it actually isn't evaluated immediately. This is a big advantage of sequences vs. lists or dictionaries...you only evaluate them when you are actually iterating over the items. This means they will generally have better performance, and it can make a large difference on huge data sets. But you could, for example, do a for loop over the result of the filter (without list), and if you break early, the check won't be done for the rest of the items. It's a subtle distinction, but if you get in the habit of using things like map, filter, reduce, etc. on sequences you can "compose" otherwise complex logic (like our original function!) into much smaller pieces that work in an intuitive manner, and you don't need to create a bunch of one-line functions for each step. This last portion is especially useful; sometimes you'll want a simple calculation throughout a function, but you don't need it elsewhere; if you define it as a lambda inside the function, you can call it multiple times without needing external helper functions that don't do a lot. If you don't get it all at first, that's fine, but hopefully I broke it down enough that you get an idea of why you might use these. I've personally found learning new concepts is easier if I understand the purpose behind them, but if that's too much, the basic idea is that lambda is a function that you define at the point you want to use it, and essentially is just a parameter list with a return statement. If you ever find yourself writing one-line functions that just return something, consider whether or not they make more sense as a lambda.
🌐
Mimo
mimo.org › glossary › python › lambda-function
Python Lambda Function: Syntax, Usage, and Examples
A Python lambda function is a small, anonymous function that you can define in a single line.
🌐
DataCamp
datacamp.com › tutorial › python-lambda-functions
Python Lambda Functions: A Beginner’s Guide | DataCamp
January 31, 2025 - Lambda functions in Python are powerful, concise tools for creating small, anonymous functions on the fly. They are perfect for simplifying short-term tasks, streamlining code with higher-order functions like map, filter, or sorted, and reducing clutter when defining temporary or throwaway logic.
🌐
DigitalOcean
digitalocean.com › community › tutorials › lambda-expression-python
Python Lambda Expressions Explained with Examples | DigitalOcean
July 8, 2025 - A lambda expression in Python is an inline, anonymous function defined with the keyword lambda, ideal for short, one‑liner operations passed as arguments—particularly to higher‑order functions like map, filter, and sorted.
🌐
Programiz
programiz.com › python-programming › anonymous-function
Python Lambda/ Function (With Examples)
In Python, a lambda function is a special type of function without the function name.
🌐
Medium
medium.com › @nirajan_DataAnalyst › lambda-function-in-python-f8d37b5c7e5e
Lambda Function in Python. A lambda function is an anonymous… | by NIRAJAN JHA | Medium
April 24, 2024 - Lambda Function in Python A lambda function is an anonymous function (i.e. defined without a name) that can take any number of arguments but, unlike normal functions, evaluates and returns only one
Top answer
1 of 7
370

The first one creates a single lambda function and calls it ten times.

The second one doesn't call the function. It creates 10 different lambda functions. It puts all of those in a list. To make it equivalent to the first you need:

[(lambda x: x*x)(x) for x in range(10)]

Or better yet:

[x*x for x in range(10)]
2 of 7
191

This question touches a very stinking part of the "famous" and "obvious" Python syntax - what takes precedence, the lambda, or the for of list comprehension.

I don't think the purpose of the OP was to generate a list of squares from 0 to 9. If that was the case, we could give even more solutions:

squares = []
for x in range(10): squares.append(x*x)
  • this is the good ol' way of imperative syntax.

But it's not the point. The point is W(hy)TF is this ambiguous expression so counter-intuitive? And I have an idiotic case for you at the end, so don't dismiss my answer too early (I had it on a job interview).

So, the OP's comprehension returned a list of lambdas:

[(lambda x: x*x) for x in range(10)]

This is of course just 10 different copies of the squaring function, see:

>>> [lambda x: x*x for _ in range(3)]
[<function <lambda> at 0x00000000023AD438>, <function <lambda> at 0x00000000023AD4A8>, <function <lambda> at 0x00000000023AD3C8>]

Note the memory addresses of the lambdas - they are all different!

You could of course have a more "optimal" (haha) version of this expression:

>>> [lambda x: x*x] * 3
[<function <lambda> at 0x00000000023AD2E8>, <function <lambda> at 0x00000000023AD2E8>, <function <lambda> at 0x00000000023AD2E8>]

See? 3 time the same lambda.

Please note, that I used _ as the for variable. It has nothing to do with the x in the lambda (it is overshadowed lexically!). Get it?

I'm leaving out the discussion, why the syntax precedence is not so, that it all meant:

[lambda x: (x*x for x in range(10))]

which could be: [[0, 1, 4, ..., 81]], or [(0, 1, 4, ..., 81)], or which I find most logical, this would be a list of 1 element - a generator returning the values. It is just not the case, the language doesn't work this way.

BUT What, If...

What if you DON'T overshadow the for variable, AND use it in your lambdas???

Well, then crap happens. Look at this:

[lambda x: x * i for i in range(4)]

this means of course:

[(lambda x: x * i) for i in range(4)]

BUT it DOESN'T mean:

[(lambda x: x * 0), (lambda x: x * 1), ... (lambda x: x * 3)]

This is just crazy!

The lambdas in the list comprehension are a closure over the scope of this comprehension. A lexical closure, so they refer to the i via reference, and not its value when they were evaluated!

So, this expression:

[(lambda x: x * i) for i in range(4)]

IS roughly EQUIVALENT to:

[(lambda x: x * 3), (lambda x: x * 3), ... (lambda x: x * 3)]

I'm sure we could see more here using a python decompiler (by which I mean e.g. the dis module), but for Python-VM-agnostic discussion this is enough. So much for the job interview question.

Now, how to make a list of multiplier lambdas, which really multiply by consecutive integers? Well, similarly to the accepted answer, we need to break the direct tie to i by wrapping it in another lambda, which is getting called inside the list comprehension expression:

Before:

>>> a = [(lambda x: x * i) for i in (1, 2)]
>>> a1
2
>>> a0
2

After:

>>> a = [(lambda y: (lambda x: y * x))(i) for i in (1, 2)]
>>> a1
2
>>> a0
1

(I had the outer lambda variable also = i, but I decided this is the clearer solution - I introduced y so that we can all see which witch is which).

Edit 2019-08-30:

Following a suggestion by @josoler, which is also present in an answer by @sheridp - the value of the list comprehension "loop variable" can be "embedded" inside an object - the key is for it to be accessed at the right time. The section "After" above does it by wrapping it in another lambda and calling it immediately with the current value of i. Another way (a little bit easier to read - it produces no 'WAT' effect) is to store the value of i inside a partial object, and have the "inner" (original) lambda take it as an argument (passed supplied by the partial object at the time of the call), i.e.:

After 2:

>>> from functools import partial
>>> a = [partial(lambda y, x: y * x, i) for i in (1, 2)]
>>> a0, a1
(2, 4)

Great, but there is still a little twist for you! Let's say we wan't to make it easier on the code reader, and pass the factor by name (as a keyword argument to partial). Let's do some renaming:

After 2.5:

>>> a = [partial(lambda coef, x: coef * x, coef=i) for i in (1, 2)]
>>> a0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() got multiple values for argument 'coef'

WAT?

>>> a[0]()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() missing 1 required positional argument: 'x'

Wait... We're changing the number of arguments by 1, and going from "too many" to "too few"?

Well, it's not a real WAT, when we pass coef to partial in this way, it becomes a keyword argument, so it must come after the positional x argument, like so:

After 3:

>>> a = [partial(lambda x, coef: coef * x, coef=i) for i in (1, 2)]
>>> a0, a1
(2, 4)

I would prefer the last version over the nested lambda, but to each their own...

Edit 2020-08-18:

Thanks to commenter dasWesen, I found out that this stuff is covered in the Python documentation: https://docs.python.org/3.4/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result - it deals with loops instead of list comprehensions, but the idea is the same - global or nonlocal variable access in the lambda function. There's even a solution - using default argument values (like for any function):

>>> a = [lambda x, coef=i: coef * x for i in (1, 2)]
>>> a0, a1
(2, 4)

This way the coef value is bound to the value of i at the time of function definition (see James Powell's talk "Top To Down, Left To Right", which also explains why mutable default values are shunned).

🌐
Trey Hunner
treyhunner.com › 2018 › 09 › stop-writing-lambda-expressions
Overusing lambda expressions in Python
You’ll typically see lambda expressions used when calling functions (or classes) that accept a function as an argument. Python’s built-in sorted function accepts a function as its key argument.
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - Python lambdas are little, anonymous functions, subject to a more restrictive but more concise syntax than regular Python functions. ... Notes: You’ll see some code examples using lambda that seem to blatantly ignore Python style best practices. This is only intended to illustrate lambda calculus concepts or to highlight the capabilities of Python lambda.
🌐
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.
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python › define lambda function handler in python
Define Lambda function handler in Python - AWS Lambda
January 31, 2026 - The Lambda function handler is the method in your Python code that processes events. When your function is invoked, Lambda runs the handler method.