Tutorials Home   >   Core Programming Concepts   >   Anonymous Functions

Anonymous Functions

1. Introduction to  Anonymous Functions

In programming, a function is a block of code designed to perform a specific task. Normally, functions are defined using a name and the def keyword (in Python) or similar syntax in other languages.

However, sometimes we need small, simple functions that are used only once and don’t need a name. This is where lambda functions or anonymous functions come in.

A lambda function is a small, unnamed function defined using the lambda keyword.

They are often used for short tasks, especially when functions are passed as arguments to other functions.


2. Syntax of Lambda Functions

General Syntax:

lambda arguments: expression
  • lambda – Keyword to define the function

  • arguments – Input parameters (can be zero or more)

  • expression – Single expression whose value is automatically returned

Important: Lambda functions can only contain a single expression, not multiple statements.


3. Basic Example

square = lambda x: x * x
print(square(5))

Output:

25

Explanation:

  • lambda x: x * x defines a function that squares a number

  • No def keyword or function name is required, though we assigned it to square for reuse


4. Lambda Functions Without a Name

Lambda functions are called anonymous functions because they don’t need a name. They can be used directly where a function is required.

Example:

print((lambda x, y: x + y)(3, 5))

Output:

8

Explanation:

  • (lambda x, y: x + y) creates an anonymous function

  • (3, 5) calls it immediately


5. Lambda Functions with Multiple Arguments

Lambda functions can take any number of arguments.

Example:

add = lambda a, b, c: a + b + c
print(add(2, 3, 4))

Output:

9

6. Lambda Functions with Default Arguments

Lambda functions can also have default values.

Example:

greet = lambda name="Student": "Hello, " + name
print(greet())
print(greet("Amit"))

Output:

Hello, Student
Hello, Amit

7. Lambda Functions in Python with map(), filter(), and reduce()

Lambda functions are commonly used as arguments to higher-order functions.

Example with map():

numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x*x, numbers))
print(squared)

Output:

[1, 4, 9, 16, 25]

Example with filter():

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)

Output:

[2, 4, 6]

Example with reduce():

from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product)

Output:

24

Explanation:

  • map() applies a function to each element

  • filter() selects elements that satisfy a condition

  • reduce() combines elements to produce a single value


8. Advantages of Lambda / Anonymous Functions

  1. Concise – Can write small functions in one line

  2. Flexible – Used wherever a function is required

  3. Anonymous – No need to create a full function name if it’s used once

  4. Useful in functional programming – Works well with map(), filter(), reduce(), and sorted()


9. Limitations of Lambda Functions

  1. Can only contain a single expression, no multiple statements

  2. Cannot include loops or assignments directly

  3. Less readable if overused in complex logic

  4. Cannot include docstrings

Tip: Use lambda functions for simple operations; use def for complex functions.


10. Lambda Functions with sorted() and key

Lambda functions are often used to customize sorting.

Example:

students = [("Amit", 16), ("Riya", 15), ("Sona", 17)]
sorted_students = sorted(students, key=lambda x: x[1])
print(sorted_students)

Output:

[('Riya', 15), ('Amit', 16), ('Sona', 17)]

Explanation:

  • key=lambda x: x[1] sorts the list of tuples by the second element (age)


11. Lambda Functions vs Regular Functions

Feature Lambda Function Regular Function (def)
Name Optional / Anonymous Required
Length Single expression Can have multiple statements
Use case Short, one-time use Complex or reusable code
Readability Compact but can be less readable Clear and documented
Return Implicit Explicit (return)

12. Common Mistakes by Beginners

  1. Trying to use multiple statements in a lambda

  2. Assigning complex logic to a lambda function

  3. Forgetting parentheses when calling an anonymous lambda

  4. Using lambda unnecessarily when a normal function is clearer


13. Best Practices for Lambda Functions

  • Use lambda functions for simple, one-line tasks

  • Avoid using lambdas for complex operations

  • Use them with higher-order functions (map, filter, reduce)

  • Keep the expression short and readable

  • Assign to a variable only if you plan to reuse it


14. Conclusion

Lambda or anonymous functions are compact, flexible tools for writing small, temporary functions. They are widely used in functional programming and when functions need to be passed as arguments.

Understanding lambda functions allows learners to:

  • Write short, readable, and reusable code

  • Use Python’s built-in higher-order functions effectively

  • Avoid cluttering code with unnecessary function definitions

Lambda functions are essential for clean and efficient programming, especially in data processing, sorting, filtering, and functional programming tasks.