Python

key parameter in Python's max() function

7 months, 1 week ago ; 256 views
Share this

key parameter in Python's max() function

In the following section, I will illustrate how you can use various other functions or lambdas as the key parameter in Python's max() function through examples that will make this real.

example of how to use key=len in max() python

The key parameter in Python's max() function makes it easier to specify a function that will be called on each element before comparison. It is particularly useful when you want to find the maximum element based on a custom criterion, such as the length of strings.

Here's an example of how to use key=len with max() to find the string with the maximum length in a list of strings:

strings = ["apple", "banana", "orange", "kiwi"]
longest_string = max(strings, key=len)
print("The longest string is:", longest_string)

In this example, key=len is passed to max(). It means that before comparing the strings in the list, the len() function will be applied to each string, and the result will be used for comparison.

As a result, max() will return the string with the maximum length from the list. So, "banana" is returned because it has the maximum length among the strings in the list.

Apart from len, you can use various other functions or lambdas as the key parameter in Python's max() function. Here are some examples:

Alphanumeric order

To find the maximum string in a list based on alphanumeric order:

strings = ["apple", "banana", "orange", "kiwi"]
max_alphanumeric = max(strings, key=str.lower)
print("Max (alphanumeric order):", max_alphanumeric)

Custom function

The max() function is flexible enough to allow you to define a custom function to extract a particular property or perform a custom comparison:

def custom_func(word):
    return word.count('a')

strings = ["apple", "banana", "orange", "kiwi"]
max_custom = max(strings, key=custom_func)
print("Max (custom function):", max_custom)

Lambda function

Alternatively, use the lambda function to perform a custom comparison inline:

strings = ["apple", "banana", "orange", "kiwi"]
max_lambda = max(strings, key=lambda x: x.count('a'))
print("Max (lambda function):", max_lambda)

Attribute of objects

If you have a list of objects and want to find the object with the maximum value of a particular attribute:

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

student = [Student("Joy", 30), Student("Abraham", 25), Student("Thomas", 35)]
oldest_student = max(student, key=lambda x: x.age)
print("Oldest student:", oldest_student.name)

 

These are just a few examples. You can use any function or lambda expression that returns a value suitable for comparison with the elements of the iterable.

 

 

 

 

 

 

Become a member
Get the latest news right in your inbox. We never spam!

Read next

Island Perimeter

 This solution seeks to find the perimeter of an island in a grid.  The problem considers a grid where each cell represents land (1) or … Read More

Kibsoft 3 months, 3 weeks ago . 76 views

Pacific Atlantic Waterflow

3 months, 3 weeks ago . 82 views