Open In App

Python – Lambda function to find the smaller value between two elements

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 5

Method 1: Using lambda and min() method

Python3




# lambda function to return minimum of
# two elements a, b are the arguments and
# min() method is the expression here.
get_min = lambda a, b : min(a,b)
  
print(get_min(5, 8))


Output:

5

Explanation: The a,b are passed to the lambda function and min() method is used as an expression to return the minimum element.

Method 2: Using lambda and ternary operator

Python3




# lambda function to return minimum of two elements
# a, b are the arguments and ternary
# operator is used to compare two elements
get_min = lambda a, b : a if a < b else b
  
print(get_min(5, 8))


Output:

5

Explanation: a, b are the arguments and ternary operator is used for comparing two elements

Method 3 :Using Tuple and lambda 

Python3




# Two lambda functions will be stored
# in tuple such that 1st element is b
# and 2nd element will be b.
# if [a<b] is true it return 1 and
# element with index 1 will print
# else if [a<b] is false it return 0,
# so element with index 0 will print
a = 5
b = 8
print((lambda: b, lambda: a)[a < b]())


Output:

5

Explanation: 

Two lambda functions will be stored in a tuple such that 1st element is b and 2nd element will be b. if [a<b] is true it return 1 and element with index 1 will print else if [a<b] is false it return 0, so element with index 0 will print.



Previous Article
Next Article

Similar Reads

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
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 | 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 &amp; 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
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
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
Nested Lambda Function in Python
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: #
2 min read
Difference between List comprehension and Lambda in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A variable representing a member of the input sequence
3 min read
Python | Find fibonacci series upto n using lambda
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1. Find the series of fibonacci numbers using lambda function. Code #1 : B
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
Practice Tags :
three90RightbarBannerImg