Lambda Functions in Python

Lambda functions, also known as anonymous functions, are small, one-time-use functions that have no name. They are defined using the lambda keyword in Python.

Lambda functions are helpful for creating quick, throw-away functions that you don't need to reuse. They allow you to define simple functions without needing to properly define a function using def.




The syntax for lambda functions in Python is:

lambda arguments: expression

The lambda keyword is used to indicate that this is a lambda function. Then the arguments are defined, followed by a colon :, followed by the expression that gets evaluated and returned.


For example:

lambda x: x * 2

This function accepts one argument x, and returns x multiplied by 2.

Lambda functions are restricted to a single expression. You can not have multiple lines of code in a lambda function.



Here are some examples of using lambda functions in Python:


Multiply argument by 2:

# Using lambda to multiply each argument by 2

multiply_by_2 = lambda x: x * 2

print(multiply_by_2(5))  # Output: 10

print(multiply_by_2(7))  # Output: 14


Sum two arguments:

# Using lambda to sum two arguments

sum_two_numbers = lambda x, y: x + y

print(sum_two_numbers(3, 5))  # Output: 8

print(sum_two_numbers(10, 20))  # Output: 30


Compare two arguments:

# Using lambda to compare two arguments

compare_two_numbers = lambda x, y: "Equal" if x == y else "Not Equal"

print(compare_two_numbers(5, 5))  # Output: Equal

print(compare_two_numbers(3, 8))  # Output: Not Equal


Using Lambda Functions with map()


Lambda functions are commonly used along with the map() function to apply a function to each element in an iterable object like a list:

items = [1, 2, 3, 4, 5]

mapped_items = map(lambda x: x*2, items)


# [2, 4, 6, 8, 10]

The map() function applies the lambda function to each element x in items, multiplying it by 2.


Using Lambda Functions with filter()


Similarly, filter() can be used with a lambda function to filter out elements from a list:

items = [1, 2, 3, 4, 5, 6]

filtered_items = filter(lambda x: x % 2 == 0, items)


# [2, 4, 6]

This filters out any elements x that do not match the condition x % 2 == 0 (even numbers).


Using Lambda Functions with reduce()


The reduce function, available in the functools module, is used for cumulative operations. Here's an example using lambda with reduce to find the sum of a list:

from functools import reduce

items = [1, 2, 3, 4]

summed = reduce(lambda x, y: x + y, items)


# 10

This recursively sums all the elements x and y in items to produce a total sum.


When to Use Lambda Functions


Lambda functions are best suited for short, simple, single-use functions. Any time you need a small anonymous function that you only need to use once, lambda functions are great.

They help reduce repetitive code and are syntactically compact.

However, anything more complex is better suited for standard def function definitions.

Lambda functions are one of the small but useful tools in the Python language for creating quick, disposable functions!


Advantages of Lambda Functions


Lambda functions offer several benefits:

  1. Conciseness: Lambda functions allow you to define simple functions in a single line of code, which can make your code more concise and readable.


  1. Avoiding Named Functions: When you have a small function that is only used once, you can avoid cluttering your code with named functions by using lambdas.


  1. Functional Programming: Lambda functions are commonly used in functional programming constructs like map, filter, and reduce.


Lambda Functions vs Regular Functions


Lambda functions have some limitations compared to regular functions created with def:

  • Single Expression: Lambda functions can only contain a single expression, while regular functions can contain multiple statements and have more complex logic.
  • No Documentation Strings: Lambda functions cannot have docstrings, making them less suitable for well-documented code.
  • Limited Readability: For complex operations, regular functions with descriptive names are often more readable and maintainable than lambda functions.




Lambda functions in Python provide a concise way to create small, disposable functions for simple operations. Their anonymous nature and single-expression constraint make them well-suited for use cases like mapping, filtering, and sorting data. However, they should be used judiciously and not as a replacement for named functions in more complex scenarios. By understanding when and how to use lambda functions, you can leverage their power to write more expressive and efficient code.

Thanks for feedback.

Read More....
Arrays and Lists in Python
Python Iterators
Most common built-in methods in Python
Python Decorators
Python Dictionaries