top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

A simple guide to Lambda Expression in Python

The lambda operator or lambda function used in python can be called a simple function without name. It is generally used when the function is needed for a temporary period of time and is used inside the other functions like filter(), map() etc. It reduces the line of code as compared to normal functions. The general syntax of a lambda function is quite simple:

lambda argument_list: expression

argument_list= represents the comma-separated list of arguments

expression= arithmethic expressions using arguments.

multiply=lambda x,y:x*y

Compare to the normal function, the above lambda expression can as be written as:

def multiply(x,y):
    return mul

Now let's use the map() function to express the lambda function

The map() function can be expressed as shown as below: r = map(func, seq) It uses two expressions, the first argument is the function and the second argument is a sequence (example list) which are applied to the function.

BMI = list(map(lambda x,y: round(x/(y*y),3), weight_of_students,height_of_students))
print("The Body Mass Index of students are",BMI)
OUTPUT: The Body Mass Index of students are [15.095, 24.444, 26.172, 16.649, 19.631]
a = [21, 51, 12]
b = [-12, -12, 1, 1]
c = [-1, -4, 5, 9]
answer=list(map(lambda x, y, z : 52*x-z/y*z, a, b, c))
OUTPUT:[1092.0833333333333, 2653.3333333333335, 599.0]

Hence in this way Lambda expression can be used to optimize and maintain better code efficiency.


Recent Posts

See All
bottom of page