How to find the max value using lambda expression in Python? You can use the max() function to find the maximum value from a given iterable, such as a list, tuple, set, string, or dictionary. You can find the maximum number efficiently in a list using a lambda expression along with the built-in max() function and the key parameter. The key parameter of the max() function allows you to define a custom comparison rule for the elements in the list.
You can find maximum value using lambda expression in Python using many ways, for example, by using max() & lambda, reduce(), and len() functions. In this article, I will explain how to find the maximum value of iterable objects using the max() function along with lambda expressions with examples.
1. Quick Examples of Finding Max Value using Lambda
If you are in a hurry, below are some quick examples of how to find the maximum value in an iterable object using lambda expressions.
# Quick examples of finding max value using lambda
# Example 1: Create max function
# Using a lambda expression
maximum = lambda x, y: x if x > y else y
result = maximum(8, 17)
# Example 2: Find the maximum value
# Using max() & lambda expression
mylist = [25, 35, 100, 10, 60]
max_number = max(mylist)
# Example 3: mylist = [“25”, “35”, “100”, “10”, “60”]
# Find the maximum value using max() function
# along with lambda expression
max_number = max(mylist, key=lambda x:int(x))
# Example 4: Find maximum value with lambda
# To sort by length of key
mystring = [“Python”,”Spark”,”Hadoop”,”Java”]
max_length_string = max(mystring, key=lambda x: len(x))
# Example 5: Using max() and lambda function
mydict = {‘Python’:2000, ‘Spark’:3000, ‘Hadoop’:2500}
max_value = max(mydict, key=lambda x: mydict[x])
# Example 6: Find list of tuples
# Using max() and lambda function
mylist = [(‘Python’, 2000), (‘Spark’,3000), (‘Hadoop’,2500)]
max_value = max(mylist, key=lambda x: x[1])
Example 7: Using reduce() function along with lambda
mylist = [25, 35, 100, 10, 60]
max_number = reduce(lambda x, y: x if x >= y else y, mylist)
2. Create Max Function Using a Lambda Expression
You can create a simple max function using a lambda expression that finds the maximum element between two elements, you can define a lambda function and call it with two arguments.
In the below example, the lambda function(lambda x, y: x if x > y else y) takes two arguments x and y and returns x if x is greater than y, otherwise, it returns y. By calling the lambda function with two values, it will compare them and return the maximum value between them. In the example, the maximum of 8 and 17 is 17, so the output is 17.
# Create max function
# Using a lambda expression
maximum = lambda x, y: x if x > y else y
result = maximum(8, 17)
print(“The maximum element between two elements is:”, result)
# Output:
# The maximum element between two elements is: 17
3. Find the Maximum Value Using max() & Lambda
You can find the maximum value in an iterable object using built-in max() along with a lambda function, you can pass the lambda function as the key parameter to the max() function. It will return the maximum value of a given iterable object.
3.1 Syntax of max() Function
Following is the syntax of the built-in max() function in Python
# Syntax of max() function
max(iterable, key)
3.2 Parameters
Iterable: Using these elements we are going to find the maximum value and this can be any iterable object such as list, tuple, or string.
Key: (optional) Is used to define custom comparison rules for the elements in an iterable object.
3.3 Return Value
It returns the maximum value in an iterable object.
Let’s create the list and pass it into max() function without passing key parameter to get the maximum value of the given list.
# Initialization the list
mylist = [25, 35, 100, 10, 60]
print(“Original list: “, mylist)
# Find the maximum value using max()
# without passing ker param
max_number = max(mylist)
print(“The maximum value is:”, max_number)
3.4 Usage of Lambda in max() Function
When we pass a list of strings instead of a list of numbers into the max() function, it will compare the elements as strings and not as numbers so, the result won’t get as expected.
Observe, the following example,
# Initialization list
mylist = [“25”, “35”, “100”, “10”, “60”]
print(“Original list: “, mylist)
# Find the maximum value
# Using max() function
max_number = max(mylist)
print(“The maximum value is:”, max_number)
# Output:
# Original list: [’25’, ’35’, ‘100’, ’10’, ’60’]
# The maximum value is: 60
The above code has been returned 60 as a maximum value instead of 100.
By using key param along with the max() function to overcome the uncorrected output of the above code. You can use the lambda function in a key parameter to compare the elements of a given list and return the maximum value among them.
The lambda expression lambda value: int(value) is used as the key parameter for the max() function. It converts each value to an integer before performing the comparison. As a result, the maximum value is correctly identified as 100, which is the largest value in the list.
# Initialization list
mylist = [“25”, “35”, “100”, “10”, “60”]
print(“Original list: “, mylist)
# Find the maximum value using max() function
# along with lambda expression
max_number = max(mylist, key=lambda x:int(x))
print(“The maximum value is:”, max_number)
Yields below output.
4. Use max() Function with Lambda over the List of Strings
You can find the maximum value in a list based on the length of a key using a lambda expression, you can use the max() function with a lambda function as the key parameter.
In the below example, the lambda function lambda x: len(x) is used as the key parameter. The lambda function takes a value x (in this case, a string) and returns its length using the len() function. The max() function compares the elements based on the length of their keys and returns the maximum value. In this case, “Python” has the longest length among the strings in the list, so it is returned as the maximum value.
# Initialization string
mystring = [“Python”,”Spark”,”Hadoop”,”Java”]
print(“Original String: “, mystring)
# Find maximum value with lambda
# To sort by length of key
max_length_string = max(mystring, key = lambda x: len(x))
print(“The maximum lenth of string is:”, max_length_string)
Yields below output.
5. Get Max Value in Python Dictionary using Lambda Function
Using max() function you can get the maximum value of the dictionary. You can use the max() function with a lambda expression to get the corresponding key of the maximum value of a given dictionary. Use a lambda function as the key argument to the max. Here, the key argument specifies a lambda function that takes each key x from the dictionary and returns the corresponding key of maximum value(mydict[x]). The max() function uses this custom key function to determine the maximum value and returns the corresponding key.
# Initialization Dictionary
mydict = {‘Python’:2000, ‘Spark’:3000, ‘Hadoop’:2500}
print(“Original dictionary: “, mydict)
# Using max() and lambda function
max_value = max(mydict, key=lambda x: mydict[x])
print(“The maximum value in dictionary is:”, max_value)
# Output:
# Original dictionary: {‘Python’: 2000, ‘Spark’: 3000, ‘Hadoop’: 2500}
# The maximum value in dictionary is: Spark
6. Find a List of Tuples Using max() & lambda Function
If you have a list of tuples and you want to find the maximum element based on the second element of each tuple, you can use a lambda expression as the key parameter for the max() function.
In the below example, the lambda function lambda x: x[1] is used as the key parameter. The lambda function takes a tuple x and returns the value at the index 1, which corresponds to the second element of the tuple. The max() function compares the tuples based on the second element, and the tuple with the maximum second element is returned.
# Initialization list
mylist = [(‘Python’, 2000), (‘Spark’,3000), (‘Hadoop’,2500)]
print(“Original list: “, mylist)
# Find list of tuples
# Using max() and lambda function
max_value = max(mylist, key=lambda x: x[1])
print(“The maximum value in list is:”, max_value)
# Output:
# Original list: [(‘Python’, 2000), (‘Spark’, 3000), (‘Hadoop’, 2500)]
# The maximum value in list is: (‘Spark’, 3000)
7. Using reduce() Function Along with Lambda
Alternatively, you can find the maximum value mylist using the reduce() function along with an lambda expression. The reduce() function from the functools module is used to perform a binary operation on the elements of the list, gradually reducing it to a single value.
In the below example, the lambda expression lambda x, y: x if x >= y else y is used to compare two elements x and y. The reduce function compares the elements of the mylist one by one, and at each step, it keeps the greater element between x and y. Finally, it returns the maximum value found.
from functools import reduce
# Using reduce() function along with lambda
mylist = [25, 35, 100, 10, 60]
max_number = reduce(lambda x, y: x if x >= y else y, mylist)
print(“The maximum value is:”, max_number)
Yields the same output as above.
8. Conclusion
In this article, I have explained the Python max() function and using lambda expression with max() how we can return the maximum value of an iterable object.
Happy Learning !!
How to find the max value using lambda expression in Python? You can use the max() function to find the maximum value from a given iterable, such as a list, tuple, set, string, or dictionary. You can find the maximum number efficiently in a list using a lambda expression along with the built-in max() function Read More Python, Python Tutorial, Pending review by admin, Python Lambda Examples