Loading, please wait...

A to Z Full Forms and Acronyms

Anonymous Function in Python | Python tutorial

This article is all about anonymous function in python, how we can use it with other built-in functions like filter(), map() and reduce().

Anonymous Function in Python

In Python, an anonymous function is a function that can be defined without a name, unlike normal functions which are defined using the ‘def’ keyword.

In python, the anonymous function is defined using the lambda keyword. Python and other languages like Java, C#, and even C have had lambda functions added to their syntax, whereas languages like LISP Or ML family of languages use lambdas as a core concept.  

Through this article, I am going to explain to you all about the anonymous function in python. The topics that we will cover through this article are as follows.

  • About Lambda function
  • Why the lambda function?
  • Using Lambda function with python built-ins
  • use of lambda function with filter()
  • use of lambda function with map()
  • use of lambda function with reduce()

a. What is a lambda function?

 This function is capable of behaving similarly to a regular function declared using the python's def keyword.

 A lambda function is a small anonymous function or in simple terms, it is a single-line function declared with no name, which can take any number of arguments but can only have one expression. The use of a lambda function helps to optimize the line of code.

Characteristics of the lambda function.

  • It’s an anonymous function.
  • A single-line function declared with no name.
  • Can take any no. of arguments but it contains a single expression.
  • Code that contains lambda function may or may not return any value.
  • It can be used to return function objects.

Syntax

 lambda arguments: expression

Where the expression is executed and the result is returned.

 let’s see through a code example:

 1. A lambda function that adds 29 to the number passed in as an argument, and print the result:

x = lambda a : a + 29

print(x(5))
#output: 34

2. Lambda functions can take any number of arguments:

 let’s see a code example: A lambda function that multiplies argument x with argument y and print the result:

mulfun = lambda x, y : x * y

print(mulfun(5, 6))
#output=30

3. A lambda function that sums argument a, b, and c and print the result:

x = lambda a, b, c : a + b + c

print(x(5, 6, 2))
#output: 13

b. Why Lambda Functions?

 The power of lambda is better shown when we use them as an anonymous function inside another function.

 let’s look at an example and try to figure out the difference between a normal function and a lambda function.

Normal function:

 def function(args):

     return retrn_value

Lambda function:

function= lambda args: retrn_value

      

c. USING LAMBDA FUNCTION WITH PYTHON BUILT-INs

lambda functions provide an elegant and powerful way to operate using built-in methods in python that is possible due to the reason that it can be invoked immediately and passed as an argument to these functions.

d. USE OF LAMBDA function with filter()

Characteristics of filter():

  • The filter function takes two arguments: the first one is function_object and the other is iterable. 
  • The function_object returns the Boolean value and is called for each element of iterable.
  • The filter function returns only those elements for which the Boolean value is True.
  • It can only have one iterable as input.
  • It returns a list of elements.

Syntax:

filter(function_object, iterable)

 Example:

The odd number using filter function:

Code: 

num= [1,2,3,4,5,6,7,8,9,12]

odd= list(filter(lambda x : x % 2 == 0, num))
print(odd)

#output: [2, 4, 6, 8, 12]

Code Explanation:

1. first we define a variable that holds the list of numbers.

2. lambda keyword is used to define an anonymous function.

3. x is the parameter that we pass to the lambda function.

4. Then we passed function_object and iterable to filter () function.

5. At last, we used the list() constructor to convert the output into a list. 

6.odd keyword is the variable that we are using to hold the result. 

e. USE OF LAMBDA function with map()

Characteristics of map() function:

  • This function accepts a function object and any number of iterables, such as a dictionary, list, etc.
  • It executes the function-object for each element in the sequence.
  • It returns a list of the elements modified by the function object.  The returned value of the map object can be passed to a list to create a list as well as to set to create a set.

Syntax :

 map(function_object , iterable1, iterable2….)

Example:

Python program to demonstrate the working of the map().

Program to find the square of a number.

Code using a normal function with map().

def square(n):

    return n*n

#list of numbers
x = (5, 9, 12, 3, 4, 7)
results=map(square, x)
print(list(results))
#output: [25, 81, 144, 9, 16, 49]

 

Code using lambda function with map().

#list of numbers

num = (5,9,12,3,4,7)
result = list(map(lambda n : n*n , num))
print(result)

#output: [25, 81, 144, 9, 16, 49]

f. USE OF LAMBDA function with reduce()

Characteristics of the reduce():

  • This function accepts a function and a list.
  • Returns a new list which contains all modified items returned by the function for each item.
  • This performs a repetitive operation over the pairs of the list.
  • Unlike map(), and filter() it returns a single value as a result.

Syntax:

reduce(function, sequence)

How does it work?

   If sequence=[s1,s2,s3,…….sn], then calling reduce function 

   reduce(function ,sequence) works like:

  • At first, fun(s1,s2) will be executed first.
  • The list of sequence will look like [fun(s1,s2),s3,s4….sn]
  • And after each call function will be applied on the previous results , it will continue like this until just one element is left and
  • return this element as the result of the reduce() function. that’s why it returns a single output.  

Python program to demonstrate the working of reduce() function.

Program to determine the sum of a list of numerical values by using reduce().

Code:

#importing reduce() from functools
from functools import reduce
#list of numbers
num=[12,23,45,67,89,90,19]
#'sum' variable that stores the result.
sum=reduce((lambda x,y: x + y), num)
#print result
print(sum)

#output: 345

Hope you like this article, for more articles on python stay tuned with us. 

Till now, Happy learning🙂🙂

A to Z Full Forms and Acronyms

Related Article