In this tutorial, we will discuss one of the main topics of python know as lambda function aka anonymous function. we will discuss how and where to use it.

Table of Content

  • What are lambda Functions
  • How to create and use a lambda function in python
  • python map function
  • python filter function

What are lambda functions in python?

In python, there is a keyword known as lambda which is used to create a user-defined function without defining the function name or any def keyword. Python lambda can create a user-defined function without the function name that’s why they also are known as an anonymous function.

How to create and use a lambda function in python:

Unlike write code in different lines to create a function using a def keyword with lambda function you can create a function in one line.

Syntax of the lambda function

lambda arguments: expression

in the above syntax, the lambda is the main keyword which we define in every lambda function and the argument are similar to the normal argument used in def functions. The expression part should be the logic because the lambda function return the expression, which means it returns the value which we write after the semicolon (:).

The question is if we are creating a function without function name so how to call it? To call a function created with lambda we need to assign a name to that lambda function.

Syntax of assign a name to lambda function

name = lambda agruments : expression               #now name has become the function.

let’s understand lambda function with an example:

Example 1

cube = lambda argument : argument * argument * argument
print(cube(3))

#Output

27

The same function using def

Example 2

def cube(argument):
    return argument*argument*argument

print(cube(3))

#Output
27

Behind the code

In example 1 we have written a lambda function which takes an argument name argument and return the value argument* argument* argument. we also have assigned a name cube to the lambda function so with the help of the name we can call it.

Though often we do not assign a name to the lambda function, we directly use it with python in-built functions such as map() and filter().

Python filter() function:

filter() is an in-built function present in python which accepts 2 values as arguments the 1 value should be a function and the second value is iterable.

The filter() pass the iterable values one by one to the function and return only those values which satisfy the function.

Syntax of filter function:

filter(function, iterable) 

Let’s understand it with an example:

lis = [1,3,5, 4, 7, 6]
filtered= list (filter (lambda x : x%2==0 , lis))
print(filtered)

#output

[4, 6]

Python map() function:

map() function is similar to the filter() function which accepts 2 arguments one is function and other is iterable the only difference is the map() function apply the function condition on every value of iterable and doe not omit any value, whereas the filter() function omit the iterable values which do not suit the function condition.

Syntax of map() function.

map(function, iterable)

Let’s understand it with an example: 

lis = [1,2,4,5,6]
lis_sq = list(map (lambda x:x*x,lis))
print(lis_sq)

#Output

[1, 4, 16, 25, 36]

Python Tutorials:

Introduction

Flow Control

Function

Leave a Reply

Your email address will not be published. Required fields are marked *