Open In App

Nested Lambda Function in Python

Last Updated : 08 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Python lambda

In Python, anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. When we use lambda function inside another lambda function then it is called Nested Lambda Function.

Example 1:




# Python program to demonstrate
# nested lambda functions
  
  
f = lambda a = 2, b = 3:lambda c: a+b+c
  
o = f()
print(o(4))


Output:

9

Here, when the object o with parameter 4 is called, the control shift to f() which is caller object of the whole lambda function. Then the following execution takes place-

  • The nested lambda function takes the value of a and b from the first lambda function as a=2 and b=3.
  • It takes the value of c from its caller object o which passes c = 4.
  • Finally we get the output which is the summation of a, b and c that is 9.

Example 2:




# Python program to demonstrate
# nested lambda functions
  
  
square = lambda x: x**2
product = lambda f, n: lambda x: f(x)*n
  
ans = product(square, 2)(10)
print(ans)


Output:

200

In the above example, when the product function is called square function gets bound to f and 2 gets bound to n which then returns a function which is bound to the product which when called with 10, x is assigned this and square is called, which returns 100 and this, in turn, is multiplied with n which is 2. So it’ll finally return 200.


Previous Article
Next Article

Similar Reads

Python | Check if a nested list is a subset of another nested list
Given two lists list1 and list2, check if list2 is a subset of list1 and return True or False accordingly. Examples: Input : list1 = [[2, 3, 1], [4, 5], [6, 8]] list2 = [[4, 5], [6, 8]] Output : True Input : list1 = [['a', 'b'], ['e'], ['c', 'd']] list2 = [['g']] Output : False Let's discuss few approaches to solve the problem. Approach #1 : Naive
7 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Python - Lambda function to find the smaller value between two elements
The lambda function is an anonymous function. It can have any number of arguments but it can only have one expression. Syntax lambda arguments : expression In this article, we will learn how to find the smaller value between two elements using the Lambda function. Example: Input : 2 5 Output : 2 Input : 7 5 Output : 5Method 1: Using lambda and min(
2 min read
Python - Lambda Function to Check if value is in a List
Given a list, the task is to write a Python program to check if the value exists in the list or not using the lambda function. Example: Input : L = [1, 2, 3, 4, 5] element = 4 Output : Element is Present in the list Input : L = [1, 2, 3, 4, 5] element = 8 Output : Element is NOT Present in the list We can achieve the above functionality using the f
2 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Difference between Normal def defined function and Lambda
In this article, we will discuss the difference between normal def defined function and lambda in Python. Def keyword​​​​​​​In python, def defined functions are commonly used because of their simplicity. The def defined functions do not return anything if not explicitly returned whereas the lambda function does return an object. The def functions m
2 min read
Lambda and filter in Python Examples
Prerequisite : Lambda in Python Given a list of numbers, find all numbers divisible by 13. Input : my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70] Output : [65, 39, 221] We can use Lambda function inside the filter() built-in function to find all the numbers divisible by 13 in the list. In Python, anonymous function means that a function is witho
2 min read
Article Tags :
Practice Tags :