Definition of Lambda function :
- A lambda function in Python is a small, anonymous function defined using the lambda keyword. It can have any number of arguments but only one expression. It's often used for short operations or as an argument in functions like map(), filter(), or sorted().
Syntax:
Example :
1. Using map():
- The map() function applies a function (in this case, a lambda function) to all items in an input list.
Example: Doubling each number in a list
Program :
2. Using filter():
- The filter() function filters items from a list based on a condition defined by a lambda function.
Example: Filtering even numbers from a list
Program and Output :
3. Using sorted():
- The sorted() function sorts a list. You can pass a lambda function as a key to customize the sorting logic.
Example: Sorting a list of tuples by the second value
Program :
Output :
0 Comments