4
Answers

How to work with lambda functions in python

Lokendra Singh

Lokendra Singh

May 08
523
1

Hi c# corner Team,

I would like to learn about how we can work with lambda functions in python?

Answers (4)
1
Jaydeep Patil

Jaydeep Patil

104 17.8k 2.6m May 08

Lambda function (or lambda expression) is a small anonymous function defined with the `lambda` keyword. It allows you to create a function without a name. Lambda functions can take any number of arguments but can only have one expression.

The syntax for a lambda function is:
lambda arguments: expression

Here's a simple example
# Example 1: Adding two numbers using a lambda function
add = lambda x, y: x + y
print(add(5, 3))  # Output: 8

# Example 2: Squaring a number using a lambda function
square = lambda x: x ** 2
print(square(4))  # Output: 16

# Example 3: Checking if a number is even using a lambda function
is_even = lambda x: x % 2 == 0
print(is_even(5))  # Output: False
print(is_even(6))  # Output: True

# Example 4: Sorting a list of tuples based on the second element using a lambda function
points = [(1, 2), (3, 1), (5, 3), (2, 4)]
points_sorted = sorted(points, key=lambda x: x[1])
print(points_sorted)  # Output: [(3, 1), (1, 2), (5, 3), (2, 4)]
 

Lambda functions are often used in situations where you need a simple function for a short period, such as when passing a function as an argument to higher-order functions like `map()`, `filter()`, and `sorted()`, or for defining quick utility functions. However, for more complex logic, it's generally recommended to use a regular named function for clarity and maintainability.

Accepted
2
Naimish Makwana

Naimish Makwana

134 13.8k 201.3k May 09

Lambda functions, also known as anonymous functions, are a feature of Python and many other programming languages. They are small, inline functions that can be defined quickly and used in places where a normal function might be cumbersome or unnecessary1.

Here’s a basic syntax of a lambda function in Python:

lambda arguments: expression

The arguments are the inputs to the function, and the expression is what the function computes and returns2.

Here are some examples of lambda functions:

  1. A lambda function that adds 10 to the number you send in:
    x = lambda a: a + 10
    print(x(5))  # Outputs: 15
    
  2. A lambda function that multiplies two arguments and returns the result:
    x = lambda a, b: a * b
    print(x(5, 6))  # Outputs: 30
    
  3. A lambda function that sums three arguments and returns the result:
    x = lambda a, b, c: a + b + c
    print(x(5, 6, 2))  # Outputs: 13
    

Lambda functions are particularly powerful when used in conjunction with functions like map(), filter(), and reduce() which expect function objects as arguments1.

For example, if you want to square all numbers in a list, you can use map() with a lambda function:

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

Remember, while lambda functions can be useful for writing quick, short functions, they have limitations, such as only being able to contain one expression and not having a return statement1. For more complex operations, it’s often better to define a full function with def.

Thanks

2
Amit Mohanty

Amit Mohanty

16 52.2k 6.1m May 08

Lambda functions are typically useful when you need a small function for a short period of time and you don't want to define a full-fledged function using the def keyword, such as for use in higher-order functions like map(), filter(), and reduce(), or as arguments to functions that take other functions as parameters, like sorted(). It's important to note that lambda functions are limited to a single expression. If you need more complex logic, it's better to define a regular named function.

Please review the following articles.
https://realpython.com/python-lambda/
https://www.freecodecamp.org/news/python-lambda-function-explained/
https://www.geeksforgeeks.org/python-lambda-anonymous-functions-filter-map-reduce/
https://www.dataquest.io/blog/tutorial-lambda-functions-in-python/
https://www.programiz.com/python-programming/anonymous-function

1
Jayraj Chhaya

Jayraj Chhaya

310 6k 94.1k May 08

Lambda functions in Python are anonymous functions that can have any number of arguments but can only have one expression. They are commonly used for short, simple operations where defining a full function is unnecessary. To create a lambda function, you use the lambda keyword followed by parameters and an expression. Here's an example of a lambda function that adds two numbers:

add = lambda x, y: x + y
result = add(3, 5)
print(result)  # Output: 8

Lambda functions are often used with built-in functions like map()filter(), and reduce() for concise and readable code. Understanding lambda functions can make your Python code more elegant and efficient.