Advertisement

Lambda Function in Python

Lambda function in Python 


In Python, a lambda function is a small anonymous function that is defined without a name. It can take any number of arguments, but can only have one expression. The syntax for a lambda function is as follows:



lambda arguments: expression

Here is an example of a lambda function that takes in two arguments and returns their sum:


Lambda function in python




>>> sum = lambda x, y: x + y >>> sum(3, 4) 7

Lambda functions are often used in conjunction with higher-order functions (functions that take other functions as arguments). For example, you might use a lambda function as the sorting key in a call to the sorted() function:



>>> names = ['Alice', 'Bob', 'Eve'] >>> sorted(names, key=lambda x: len(x)) ['Bob', 'Eve', 'Alice']


Lambda functions can be useful when you need a simple function for a short period of time. However, for more complex functions, it is usually clearer to define a regular function using the def keyword.


One important feature of lambda functions is that they are limited to a single expression. This means that they cannot contain statements or annotations, and they cannot contain return statements (since the expression is automatically returned). Here is an example of a lambda function that tries to do too much and will generate a syntax error:



>>> broken = lambda x: ... if x > 0: ... return 'positive' File "<stdin>", line 2 if x > 0: ^ SyntaxError: invalid syntax

To define a more complex function, you can use the def keyword and a full function definition. For example, here is the same function as above, but defined using def:



def classify(x): if x > 0: return 'positive' elif x < 0: return 'negative' else: return 'zero'


Using def allows you to include statements, annotations, and a return statement in your function definition. It also allows you to give the function a name, which can make your code easier to understand.


Another important difference between lambda functions and regular functions is that lambda functions are defined in a single line, whereas regular functions can span multiple lines. This can make lambda functions more concise, but it can also make them harder to read if they are too long or complex.


Lambda functions are often used in conjunction with other built-in functions, such as map(), filter(), and reduce(). These functions are part of the Python standard library and are designed to work with iterables (such as lists, tuples, and generators).


The map() function applies a function to each element of an iterable and returns a new iterable with the modified elements. For example, you can use a lambda function to multiply all the elements in a list by 2:



>>> numbers = [1, 2, 3, 4] >>> result = map(lambda x: x * 2, numbers) >>> result [2, 4, 6, 8]


The filter() function filters an iterable by removing elements that do not satisfy a predicate (a function that returns a Boolean value). For example, you can use a lambda function to filter a list of numbers to only include even numbers:



Post a Comment

0 Comments