Lambda in Python
Lambda functions, also known as anonymous functions, are an elegant and compact way to define small functions without explicitly using def. They are particularly useful when a function is only needed once and does not require reusability.
1. Basics: Lambda vs. Defined Function
The following example defines a simple function that converts a number into a string:
from typing import Callable
def int_to_Char(n: int) -> str:
return n * 'Char '
print(int_to_Char(10))
#CharCharCharCharCharCharCharCharCharCharThis function can be simplified using a lambda function:
int_to_Char2: Callable[[int], str] = lambda n: n * 'Char '
print(int_to_Char2(10))
#CharCharCharCharCharCharCharCharCharCharComparison:
- def function: Multi-line, more readable, easier to debug
- lambda: Single-line, compact, but harder to understand in complex expressions
2. Lambda Functions in map()
The map() function applies a function to a sequence:
mapped = map(int_to_Char, [1,2,3,4]) # Reusable function
list(mapped)
#['Char', 'CharChar', 'CharCharChar', 'CharCharCharChar']If the function is only needed once, a lambda function is often more practical:
mapped = map(lambda n: n * 'Char ', [1,2,3,4]) # One-time use
print(list(mapped))
#['Char', 'CharChar', 'CharCharChar', 'CharCharCharChar']Comparison:
- Regular function: Useful for multiple uses
- Lambda: Practical for single-use cases
3. Using starmap() with Lambda
The starmap() function, combined with lambdas, provides an elegant solution for operations with tuples:
from itertools import starmap
def multiply_str(text: str, n: int) -> str:
return n * text
different_tuples = [('my-python-',3), ('will-',4), ('be-',5), ('better-',5)]
starm = starmap(multiply_str, different_tuples)
print(list(starm))
#['my-python-my-python-my-python-', 'will-will-will-will-', 'be-be-be-be-be-', 'better-better-better-better-better-']Alternatively, we can use a lambda function directly in starmap():
starm = starmap(lambda text, n: n * text, different_tuples) # Lambda instead of explicit function
print(list(starm))
#['my-python-my-python-my-python-', 'will-will-will-will-', 'be-be-be-be-be-', 'better-better-better-better-better-']Comparison:
- def function: Allows reusability and better readability
- lambda: Direct application, less code, but can be harder to understand
4. Executing Nameless Lambda Functions Directly
Lambda functions can be executed immediately:
(lambda s: print(f'{(s * 2).capitalize()}!!'))('nameless ')
#Nameless nameless !!Advantages:
- Immediate execution without explicit function declaration
- Compact and flexible
Get Tom Kaminski’s stories in your inbox
Join Medium for free to get updates from this writer.
Disadvantages:
- Reduced readability
- Not suitable for complex logic
Conclusion
Lambda functions are powerful when it comes to compact and one-time operations. However, they can be difficult to read, especially in complex scenarios. So, when should you use lambda?
✅ Best suited for:
- Short, one-time operations in map(), filter(), and starmap()
- Inline calculations without complex logic
❌ Not recommended for:
- Functions that need to be reused
- Complex logic that requires debugging and readability
By using lambda functions strategically, you can make your code more concise without compromising maintainability.





