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


Lambda functions are popular functions in Python which have no name. They are designed to help us when we need to apply any small operation but we know we won't be reusing the code elsewhere. In this article we will learn how to find the smaller value between two elements using the lambda function. We will explore several methods like min, sorted etc along with the lambda function to perform the same.

Using The if else Statement

The if else statement is called the conditional operator in Python. It gives us the ability to make certain decisions based on certain criteria. The conditional expression evaluates whether the result of the conditional expression is True or False. If it is True then a block of code will be executed else it will be blocked.

Example

In the following example we have used the conditional expression along with the lambda function. We have used the conditional expression to check the lowest number between a and b. We defined the lambda function and passed the value of a and b to it and returned the result obtained from the lambda function.

def smaller(a,b):
    smaller = lambda a, b: a if a < b else b
    result = smaller(a, b)
    return result
a=45
b=24
print("The smaller number between {} and {} is: {}.".format(a, b, smaller(a,b)))  

Output

The smaller number between 45 and 24 is: 24

Using The min Function

The min function in Python returns the smallest value among the series of elements.

Syntax

min(arg1, arg2, ..., argN, key=func)

Here arg1, arg2, ..., argN are the arguments or the elements. We can also pass any iterable object to the function instead of the series of arguments. The 'key' specifies any custom ordering that needs to be done if we want to use any complex data structure and do not want to use the custom ordering.

Example

In the following example we have used the min function to determine the smaller of the two numbers a, and b. We used the lambda function and wrapped up the expression in it. Next we called the lambda function and passed the arguments a, and b to it.

def find_smaller(a, b):
    smaller = lambda a, b: min(a, b)
    return smaller(a, b)
a = 47
b = 24
result = find_smaller(a, b)
print("The smaller number between {} and {} is: {}.".format(a, b, result))

Output

The smaller number between 47 and 24 is: 24.

Using Sorted Method

The sorted function in Python is an in−built function. It allows us to sort a sequence or an object in an iterable order. It returns a new sorted list without changing the original list.

Syntax

sorted(iterable, key=key, reverse=Boolean value)

Here the sorted is the name of the function. The iterable is the name of the iterable object. The key is defined as the parameter to specify the attribute which we need to use to sort the iterable if the iterable object is quite complex. The reverse parameter takes boolean True or False. If set to True it will sort in reverse order.

Example

In the following example we have used the sorted method. We used the lambda function to apply the sorted method to the elements a and b. Next we called the lambda function and passed the parameters a, and b.

def find_smaller(a, b):
    smaller = lambda a, b: sorted([a, b], key=lambda x: x)[0]
    return smaller(a, b)
a = 455
b = 89
result = find_smaller(a, b)
print("The smaller number between {} and {} is: {}.".format(a, b, result))

Output

The smaller number between 455 and 89 is: 89.

Using Lambda And Reduce Method

The 'reduce' is not an in−built method in Python. It comes under the functools module. The functools module provides various methods to deal with the functions. It helps to apply specific functions in an iterable way. It takes two required parameters: first is the name of the function and next is the iterable object.

Example

In the following example we have first imported the functools method. Next we have created a list containing two elements a, and b. We used the lambda function to develop a conditional expression which evaluates the smaller number among a, and b. Next we used the reduce method to apply this lambda function to the elements.

from functools import reduce
def find_smaller(a, b):
    elements=[]
    elements.append(a)
    elements.append(b)
    smaller = lambda a, b: a if a < b else b
    result = reduce(smaller, elements)
    return result
a = 4557
b = 66
result = find_smaller(a, b)
print("The smaller number between {} and {} is: {}.".format(a, b, result))

Output

The smaller number between 4557 and 66 is: 66.

Conclusion

In this article we understood how to find the smaller value between two values in Python. We used the lambda function to deal with it which offers an elegant way to write the codes. We used several methods of Python like reduce, sorted etc to deal with it. Since its a simple comparison between two numbers we hardly need to take care of time complexity and optimizing the algorithm.

Updated on: 18-Jul-2023

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements