Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

What is Anonymous Function or Lambda Function in Python?

Anonymous Function or Lambda Function in Python

Condition for Anonymous Function or Lambda Function in Python

  • Description: A lambda function in Python is a concise, anonymous function defined with the lambda keyword.Unlike regular functions, lambda functions do not require a name and can be used inline for small,simple operations.
Step-by-Step Process
  • Lambda can have multiple arguments but only one expression.
  • The expression is evaluated and returned automatically when the lambda function is called.
  • Lambda functions are commonly used in functions like map(), filter(), and reduce() to process data in a more compact form.
Sample Code
  • # Lambda function
    print("Output for using lambda function")
    max_number = lambda a, b: a if a > b else b
    result = max_number(10, 20)
    print(result)
    print()
    # Using lambda with filter to get even numbers
    print("Output for lambda function using filter keyword")
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    even_numbers = filter(lambda x: x % 2 == 0, numbers)
    print(list(even_numbers))
Screenshots
  • Lambda Function