Lambda in Python defines a small, anonymous function in a single line. It takes input arguments and returns a result using a single expression-no def keyword, no name, no explicit return.
Lambda expressions are ideal for short operations passed as arguments to higher-order functions like map(), filter(), or sorted(). They're not meant for complex logic or reuse.
For example, lambda x: x * 2 creates a function that doubles any number passed to it. This is equivalent to:
def double(x):
return x * 2
But written in one line without a name.
Lambda evaluates the expression immediately and returns the result. The syntax is strict: lambda [arguments]: expression.
Arguments are separated by commas. There's no assignment operator (=) or colon after the arguments. The expression after the colon runs once and returns a value.
Consider this example:
add = lambda a, b: a + b
Calling add(3, 5) returns 8. The function exists only as the value of the variable add.
Lambdas shine when you need a quick function as input to another function. For instance, sorting a list of tuples by the second element:
students = [('Alice', 85), ('Bob', 90), ('Charlie', 78)]
sorted(students, key=lambda student: student[1])
This returns [('Charlie', 78), ('Alice', 85), ('Bob', 90)]-sorted by grade.
Another use: filtering even numbers from a list:
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda n: n % 2 == 0, numbers))
The result is [2, 4, 6].
Lambdas avoid defining a full function when the logic is trivial. They're especially useful in functional programming patterns.
Lambdas are limited to a single expression. You cannot use statements like print(), return, if blocks, or try/except inside them.
They can't contain multiple lines or assignments. This restriction ensures they remain concise and focused.
| Feature | Lambda | Regular Function |
|---|---|---|
| Can have multiple statements? | No | Yes |
| Can use return? | No (expression only) | Yes |
| Can be assigned to a variable? | Yes | Yes |
| Can be used in map/filter/sorted? | Yes | Yes |
These limits prevent misuse. A lambda should do one thing well. If you need more, write a regular function.
Many assume lambdas are faster than regular functions. That's false. Performance is nearly identical. The difference is syntactic convenience.
Some think lambdas are always better for short tasks. But readability often suffers. A named function improves clarity, especially when reused.
For example, this lambda:
lambda x: x > 100 and x < 200 and x % 2 == 0
is harder to read than:
def is_valid_number(n):
return 100 < n < 200 and n % 2 == 0
Even though both do the same thing.
A non-obvious rule: lambdas don't capture variables by value-they capture by reference. This causes issues in loops:
functions = []
for i in range(3):
functions.append(lambda: i)
Calling each function returns 2-not 0, 1, 2. Because i is captured by reference, and its final value is 2.
To fix this, bind the value at creation:
functions = []
for i in range(3):
functions.append(lambda x=i: x)
Now each function returns its intended value.
Use lambdas when passing a simple function as an argument. Use regular functions when logic grows beyond one line, when you need documentation, or when the function may be reused.
Python's official style guide, PEP 8, says: "Avoid using lambda expressions for complex logic." It recommends naming functions.
According to the Python documentation, lambdas are "small anonymous functions" meant for simple cases. They're not replacements for proper functions.
I once debugged a production script where a lambda was used inside a loop to generate event handlers. After hours, we found the closure issue. Rewriting it with a named function made the bug obvious-and fixed it in minutes.
While lambdas are lightweight, they're not magic. Their power lies in simplicity and integration with Python's built-in tools-not in replacing structured code.
Use lambda in Python to write compact, readable code for simple transformations. Avoid overusing it. When in doubt, prefer a named function.