Python lambda Functions – Anonymous Power in One Line
Last updated 5 months, 1 week ago | 362 views 75 5

Python’s lambda
keyword allows you to write anonymous, one-line functions. These are useful for short, throwaway functions where using def
might feel too heavy.
In this article, you’ll learn:
-
What
lambda
functions are -
Syntax and how they compare to regular functions
-
Practical use cases
-
Common pitfalls
-
Tips and best practices
-
A complete example at the end
✅ What Is a lambda
Function?
A lambda
function in Python is a small, anonymous function defined using the lambda
keyword. It can have any number of arguments, but only one expression.
It’s typically used for:
-
Inline function definitions
-
Short, simple operations
-
Passing functions as arguments
Syntax
lambda arguments: expression
This is equivalent to:
def function_name(arguments):
return expression
Basic Example
square = lambda x: x * x
print(square(5))
Output:
25
Equivalent using def
:
def square(x):
return x * x
Multiple Parameters
add = lambda a, b: a + b
print(add(3, 4)) # 7
Lambda with map()
, filter()
, reduce()
1️⃣ map()
: Apply a function to every item in an iterable
nums = [1, 2, 3, 4]
squared = list(map(lambda x: x ** 2, nums))
print(squared) # [1, 4, 9, 16]
2️⃣ filter()
: Filter items based on a condition
nums = [1, 2, 3, 4, 5]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # [2, 4]
3️⃣ reduce()
: Cumulative reduction (needs functools
)
from functools import reduce
nums = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, nums)
print(product) # 24
Sorting with Lambda
pairs = [(1, 'b'), (2, 'a'), (3, 'c')]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print(sorted_pairs)
Output:
[(2, 'a'), (1, 'b'), (3, 'c')]
Lambda Inside List Comprehension
funcs = [lambda x: x + n for n in range(3)]
print([f(10) for f in funcs]) # Output?
Tricky Output:
[12, 12, 12]
Because lambda x: x + n
captures the reference to n
, not its value at each loop.
✅ Fix using default arguments:
funcs = [lambda x, n=n: x + n for n in range(3)]
print([f(10) for f in funcs]) # [10, 11, 12]
Full Practical Example: Sorting a List of Dictionaries
people = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35}
]
# Sort by age
sorted_people = sorted(people, key=lambda person: person["age"])
for person in sorted_people:
print(person)
Output:
{'name': 'Bob', 'age': 25}
{'name': 'Alice', 'age': 30}
{'name': 'Charlie', 'age': 35}
⚠️ Common Pitfalls
Mistake | Why It Happens | Fix |
---|---|---|
Too much logic in a lambda |
lambda supports only one expression |
Use def for complex logic |
Confusing closures (e.g., in loops) | Late binding captures variable reference | Use default args in lambda (e.g., n=n ) |
Ignoring readability | Overusing lambda can make code confusing |
Use only for short, simple expressions |
Tips and Best Practices
-
✅ Use
lambda
only when defining short, simple functions. -
✅ Prefer
def
when:-
The function has multiple expressions
-
You need docstrings or annotations
-
You want to reuse the function across many places
-
-
✅ Combine with
sorted()
,map()
,filter()
for quick tasks
Summary Table
Concept | Example |
---|---|
One-arg lambda | lambda x: x + 1 |
Two-arg lambda | lambda x, y: x * y |
Use with map() |
map(lambda x: x+2, [1,2,3]) |
Use with filter() |
filter(lambda x: x>0, [-1,0,1]) |
Use with sorted() |
sorted(data, key=lambda x: x[1]) |
What’s Next?
After learning lambda
, you can explore:
-
map()
,filter()
, andreduce()
in detail -
List comprehensions vs
map()
withlambda
-
Closures and decorators (which may use
lambda
) -
Generator expressions for efficient iteration