I want to know what exactly is lambda
in python? and where and why it is used.
thanks

- 40,161
- 57
- 141
- 175
-
4Did you take a look into the Python tutorial? http://docs.python.org/tutorial/controlflow.html#lambda-forms – Mar 08 '11 at 14:05
-
2SO needs a close reason "question is answered by first Google hit". – Glenn Maynard Mar 08 '11 at 14:19
-
4No, SO should be the first google hit... – Andrew Jaffe Mar 08 '11 at 14:23
-
2Not when there are dozens of perfectly good explanations, tutorials, references and specifications already available on a topic. – Glenn Maynard Mar 08 '11 at 14:25
-
1(To be fair, most Google hits are conflating "function" with "expression" as if they're the same thing, as are most answers below--they're not.) – Glenn Maynard Mar 08 '11 at 14:40
-
@Glenn: Are you saying that lambdas aren't functions? Simple expressions don't take arguments. – recursive Apr 14 '11 at 20:27
-
@recursive: The arguments to an expression are the variables it uses. – Glenn Maynard Apr 15 '11 at 05:35
-
Does this answer your question? [What is key=lambda](https://stackoverflow.com/questions/13669252/what-is-key-lambda) – Pac0 Dec 11 '20 at 15:49
-
Does this answer your question? [Why are Python lambdas useful?](https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful) – Georgy Dec 13 '20 at 14:57
12 Answers
Lambda is more of a concept or programming technique then anything else.
Basically it's the idea that you get a function (a first-class object in python) returned as a result of another function instead of an object or primitive type. I know, it's confusing.
See this example from the python documentation:
def make_incrementor(n):
return lambda x: x + n
f = make_incrementor(42)
f(0)
>>> 42
f(1)
>>> 43
So make_incrementor creates a function that uses n in it's results. You could have a function that would increment a parameter by 2 like so:
f2 = make_incrementor(2)
f2(3)
>>> 5
This is a very powerful idea in functional programming and functional programming languages like lisp & scheme.
Hope this helps.

- 6,504
- 4
- 30
- 37
-
1Functions in Python aren't different than other objects, and both have the same relation to variables. By the way, `lambda`s returned directly from another function seem to not be the most common example of `lambda` usage - Google codesearch says that they account for only 5% of `lambda` use. – Rosh Oxymoron Mar 08 '11 at 14:32
-
Thanks for the clarification. I'll see if I can update my attempt at an answer to make it more clear. As for the example, it was chosen by the python documentation. ;-) It's just a basic example to help someone who's asking for an introduction to the concept in python. – Dave Mar 08 '11 at 16:42
-
@Dave excuse me but isnt that the same operation as with a static variable in C/C++ ? It keeps the same value etc. Is lambda a way to simulate static variables in python? Thanks in advance! – BugShotGG Oct 05 '12 at 14:38
-
1@GeoPapas sorry fro the very late response =D. No, static variables and lamdas are different. First a static variable is still a variable while lambda is a function. Image having 3 parameters to the lambda function or a complex data structure like a list. You could have complex business logic do whatever in the lambda function. Also, there is only one static variable, while you could have many (infinite) lambdas. Image f2 in the example above and also `f3 = make_incrementor(3)`. f2 would add 2 to x and f3 would add 3. 2 different lambdas each width different valued bound to parameter n. – Dave Jan 22 '15 at 18:41
Lambdas are not anonymous functions. Lambdas are anonymous expressions.
They're accessed like functions, but they're not the same thing. Functions allow complex tasks: flow control, variable declarations and lists of statements containing expressions. Expressions are merely one part of a function, and that's what lambdas give you. They're severely limited compared to functions.
Python does not support anonymous functions. For examples of languages that do, see Javascript and Lua.
(Note: It's correct to call lambdas anonymous functions in functional languages, where the mathematical definition of "function" is used, but in procedural languages the word has a very different meaning than in mathematics.)

- 55,829
- 10
- 121
- 131
-
+1, `Lambdas are not anonymous functions. Lambdas are anonymous expressions`. So, if I understand correctly, one could say then that `in Python, with lambda, you are able to parameterize your expression`? Thanks – eat Mar 08 '11 at 15:37
-
@eat: Expressions are always parameterized--the parameter to `x*2` is `x`. Lambdas let you encapsulate expressions as an object. – Glenn Maynard Mar 08 '11 at 15:59
-
`parameter to x*2 is x`, but I don't see it like that (rather parameter in this case is constant 2). Consider `line= lambda a, b: a* x+ b`, now I prefer to say that I have an expression `a* x+ b`, with parameters `a` and `b`, meaning that `x` is bound when `line` is defined and rest of the symbols are binded when `line`is actually called. My 2 cents about lambdas. Thanks – eat Mar 08 '11 at 17:45
lambda
allows you define simple, unnamed functions inline with your code, e.g. as an argument. This is useful if you plan to use the function only once, and therefore don't want to clutter your code with a named function.
Let's say you have a list of numbers (thelist
), and you want to get a list of the same length, with each number doubled. Rather than defining a times_two
function, you could do something like this:
map(lambda x: x * 2, thelist)
lambda
also makes Currying more convenient.

- 9,133
- 9
- 39
- 59

- 4,558
- 4
- 36
- 50
Lambda functions are not a Python specific concept, but are a general programming term for anonymous function, i.e. functions without a name. In Python they are commonly used where you need to pass a simple function as a parameter to another function.
The sort
method on lists takes a parameter key
which is a function that is used to calculate the value the list is sorted on. Imagine you are sorting a list of two element tuples, and you want to sort the list based on the first element. You need to pass a function to key
which returns the first element. You could do this:
def first_element(x):
return x[0]
my_list.sort(key=first_element)
or, much more concisely you can do:
my_list.sort(key=lambda x: x[0])

- 10,682
- 3
- 35
- 38
The lambda
construct is a shorter way to define a simple function that calculates a single expression. The def
statement can be inconvenient and make the code longer, broken up and harder to read through. The functions created by the two are virtually the same by the way they work, the difference is that lambda
is limited to a single expression the value of which is returned and that def
assigns a name to the function and adds it to the local variables by that same name. Also lambda
can be used in an expression directly, while def
is a statement.
def f(x, y):
return x + y
Would give you almost the same result as
f = lambda x, y: x + y
And you can use it directly in an expression
g(5, 6, helper=lambda x, y: x + y)
which with def
would be less concise
def helper_function(x + y):
return x + y
g(5, 6, helper=helper_function)

- 20,355
- 6
- 41
- 43
-
You say your first examples for 'def' and 'lambda' would give you _almost_ the same result. How would the results differ? – Andrew Mar 09 '11 at 21:00
lambda
is a way of defining an anonymous in-line function to accomplish some task versus defining it with the def
keyword and calling it. Think of it as a shorthand you can use when you won't need to reuse a function. Anywhere you use lambda, you could substitute an explicitly called function: it's use is completely optional and left to the programmer as a matter of coding style.
Example code (without lambda):
def numSquared(num):
return num**2
for i in range(1,11):
print numSquared(i)
Example code (with lambda):
for i in range(1,11):
print (lambda x: x**2)(i)
A good beginner's discussion of lambda
:
DiveIntPython.org - Lambda

- 398,270
- 210
- 566
- 880

- 5,777
- 1
- 32
- 41
Lambda can be interpreted intuitively as algebra function.
Suppose a basic algebra y = x**2 + 1 if typed directly,
>>> y = x**2 + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
error occurs and reports that variable 'x' should be defined firstly. nevertheless,'x' is unknown, how can it be pre-defined.
Lambda helps:
python
>>> y = lambda x: x**2 + 1 # no errors reports
>>> y(3)
10
That's core about lambda,
It's in programming language to write an inline function.

- 19,953
- 19
- 81
- 138
Lambda is an anonymous function in Python programming language, instead of bearing a def statement in the front it is simply called and written lambda.
def mult2(x):
return x*2
print(mult2(2))
# returns 4
Exactly same you can do with a lambda function.
lam = lambda x : x*2
print(lam(2))
# returns 4 For more details you can look up in this blog post. http://friendlypython.herokuapp.com/2017/1/anonymous-function-lambda/ or here

- 159
- 2
- 3
lambda
is an anonymous function, usually used for something quick that needs computing.
Example (This is used in a LexYacc command parser):
assign_command = lambda dictionary, command: lambda command_function: dictionary.setdefault(command, command_function)
This is a decorator. Put before a command we'd have from the LexYacc parser:
@assign_command(_command_handlers, 'COMMAND')
This essentially builds a python script from the LexYacc language defined.
Or, a bit simpler:
`x = lambda x: return x > 0 and x & (x-1) == 0

- 4,583
- 2
- 22
- 25
I would like to explain this with a little layman approach in terms of the usage perspective, since all the technical points are already covered.
A lambda function is just like any other function, only thing that makes it different (other than the syntax and the fact that it can't be reused), is that it is used when we want to quickly write a function which takes an argument and just returns a value using only one expression.
For instance: If all that a function does is take a argument and add 1 to it then using a lambda function is a better approach than a normal function. But if your function requires more than one line of processing before returning the value, then we need to use a normal function definition and then call it.

- 323
- 2
- 12