This is a commonly asked interview question because many developers confuse the two โ
but the truth is: in Python, lambda functions are a type of anonymous function.
Letโs break this down clearly ๐
๐น 1. What is an Anonymous Function?
An anonymous function is simply a function without a name.
In Python, you cannot define truly nameless functions using def,
so Python provides a special syntax โ lambda โ to create anonymous functions.
๐ In short:
Every lambda function is an anonymous function,
but not all anonymous functions (in general programming) are necessarily lambda functions.
๐น 2. What is a Lambda Function?
A lambda function is a small, single-line anonymous function defined using the lambda keyword.
It can take any number of arguments but can only have one expression (no statements).
โ Syntax:
lambda arguments: expression๐งฉ Example:
add = lambda x, y: x + y
print(add(5, 10))Output:
15โ Here:
lambda x, y: x + ydefines an anonymous function.- Itโs assigned to the name
add(for later use). - But internally, the function has no defined name โ itโs still anonymous.
๐น 3. Named Function (Regular Function) vs Lambda Function
Using def (Regular Function):
def add(x, y):
return x + y
print(add(5, 10))Using lambda (Anonymous Function):
print((lambda x, y: x + y)(5, 10))Output:
15โ
Both perform the same task,
but the lambda function is anonymous (not bound to a name unless assigned).
๐น 4. Where Are Lambda (Anonymous) Functions Used?
Lambda functions are commonly used as short, throwaway functions, especially in:
โ (1) Functional Programming Tools
Used with built-in functions like map(), filter(), and reduce().
nums = [1, 2, 3, 4, 5]
# Double each number
doubled = list(map(lambda x: x * 2, nums))
print(doubled)Output:
[2, 4, 6, 8, 10]โ (2) Sorting with Custom Keys
data = [('Alice', 25), ('Bob', 20), ('Charlie', 30)]
data.sort(key=lambda x: x[1])
print(data)Output:
[('Bob', 20), ('Alice', 25), ('Charlie', 30)]โ (3) Filtering Values
nums = [1, 2, 3, 4, 5, 6]
even = list(filter(lambda x: x % 2 == 0, nums))
print(even)Output:
[2, 4, 6]โ (4) Inline Use (Immediate Invocation)
print((lambda x: x * x)(6))Output:
36๐น 5. Limitations of Lambda Functions
| Limitation | Explanation |
|---|---|
| Single Expression Only | Canโt include multiple statements or assignments. |
| No Name (Anonymous) | Hard to debug or reuse. |
| No Docstrings | You canโt describe what it does. |
| Not Ideal for Complex Logic | Use def for readability. |
๐งฉ Invalid Example:
lambda x: y = x + 1 # โ SyntaxError โ canโt assign inside lambdaโ Use:
def add_one(x):
y = x + 1
return y๐น 6. Key Difference Table
| Feature | Anonymous Function | Lambda Function |
|---|---|---|
| Definition | Any function without a name | A specific type of anonymous function defined using the lambda keyword |
| Syntax | Depends on the language | lambda arguments: expression |
| In Python | Implemented using lambda | The only way to create an anonymous function |
| Can Have a Name? | No | No (unless assigned to a variable) |
| Number of Expressions | Usually one (single expression) | Only one expression allowed |
| Return Keyword | Implicit (automatically returns expression value) | Implicit |
| Use Case | When you need a short-lived or inline function | Used inside map(), filter(), sorted(), etc. |
| Example | add = lambda x, y: x + y | lambda x, y: x + y |
๐น 7. Practical Comparison Example
โ Regular Function:
def square(x):
return x * x
print(square(4))Output:
16โ Lambda (Anonymous) Function:
print((lambda x: x * x)(4))Output:
16โ Both give the same result, but the second one uses an anonymous function for one-time use.
๐งพ Summary Table
| Property | Anonymous Function | Lambda Function |
|---|---|---|
| Meaning | Function without a name | Anonymous function created using lambda |
| Keyword | โ | lambda |
| Return Statement | Implicit | Implicit |
| Statements Allowed | Not allowed | Not allowed |
| Lines of Code | Usually single line | Single expression only |
| Used For | Short, one-time operations | Inline or quick operations (e.g., map, filter, sorted) |
| Example | (lambda a, b: a + b)(2, 3) | (lambda x: x ** 2)(5) |
โ In short:
- Anonymous Function = Any function without a name.
- Lambda Function = A specific anonymous function created using the
lambdakeyword.- Lambda functions are short, single-expression, inline functions โ great for one-time use.
๐งฉ Final Example
# Named Function
def multiply(x, y):
return x * y
# Anonymous (Lambda) Function
product = lambda x, y: x * y
print(multiply(3, 4)) # Using normal function
print(product(3, 4)) # Using lambdaOutput:
12
12โ
Both work the same โ
but the lambda function version is anonymous, concise, and best suited for short-term use.
