Lambda function:

  • A lambda function is an anonymous function (a function without a name).
  • Define using the ‘lambda‘ keyword.
  • Lambda functions can have any number of arguments.
  • A lambda function is limited to a single expression.
  • It is often used for a short period of time &  simple operations.

Syntax:

lambda arguments: expression
  • arguments: Input parameters.
  • expression: The operation to be performed, which will be returned as the output.

Uses of Lambda Functions:

  1. Short Anonymous Functions: Lambda functions are useful when you need a small function for a short period of time and don’t want to define a full function using ‘def’.
  2. Functional Programming: Commonly used with functions like ‘map()’, ‘filter()’, and ‘reduce()’ to apply a function to an iterable.
  3. Sorting: Often used with sorting functions to provide custom sort keys.
  4. Inline Operations: Ideal for operations that are small and won’t be reused elsewhere.

Examples:

# A lambda function to multiply by 100

Multiplyby100 = lambda x: x * 100

Multiplyby100(35)
Output:
3500
# Using Mapping function with above lambda function

my_list = [20,30,40,50,100]

list(map(Multiplyby100,my_list)) 
Output:
[2000, 3000, 4000, 5000, 10000]
#Using filter function with lambda

my_list = [5,6,7,8,9,10,11,12,13,14,15]

even = lambda x : x%2==0

list(filter(even,my_list))
Output:
[6, 8, 10, 12, 14]

Register

Login here

Forgot your password?

ads

ads

I am an enthusiastic advocate for the transformative power of data in the fashion realm. Armed with a strong background in data science, I am committed to revolutionizing the industry by unlocking valuable insights, optimizing processes, and fostering a data-centric culture that propels fashion businesses into a successful and forward-thinking future. - Masud Rana, Certified Data Scientist, IABAC

© Data4Fashion 2023-2024

Developed by: Behostweb.com

Please accept cookies
Accept All Cookies