In this article we will discuss detailed features of Python’s max() function with examples.

Python provides a max() function to find out the largest element from a collection of elements,

  • Arguments:
    • Iterable : An Iterable object like list, tuple etc.
    • arg1, arg2 … : Multiple elements
    • key : A function that will be applied to each item in the Iterable and it returns a value based on the passed argument.
  • Returns:
    • It returns the element with maximum value in the Iterable or given elements. If key function is not provided then it directly compare the given items to find out the maximum value. If key function is provided then instead of comparing items directly it will call the key function on each item and then compare it with others.

Let’s see how to use the max() function.

Using max() function with Iterable

Find maximum value in a list using max()

Suppose we have a list of numbers i.e.

As list is an Iterable, so to find the maximum value in the list we can directly pass it to the max() function i.e.

Output:

Find the character with max value in a String using max()

Suppose we have a string i.e.

As String is an Iterable, so to find the character with maximum ASCII value in the string we can directly pass it to the max() function i.e.

Output:

max() function compared the characters in the string based on their ASCII value and returned the character with largest ASCII value.

Find max string from list of Strings based on alphabetical order using max()

Suppose we have a list of strings i.e.

As list is an Iterable, so to find the maximum string based on alphabetical order in the list we can directly pass it to the max() function i.e.

Output:

Using max() function with Iterable & Custom comparater / key function

Till now we have seen examples where we are using max() function with default comparator i.e. it will use < operator to compare the elements while searching for max value. What if we want to find max element based on our custom logic or custom comparator.
Let’s see how to do that,

Find max length string from list of Strings using max()
Suppose we have a list of strings i.e.

Now we want to find the string with maximum size in this list of string. For that we need to pass the key argument to the max function i.e.

Output:

Find item in a dictionary with maximum value using max()

Output:

Some Important Points:

Max function can find max element from similar type elements. If we pass different type elements then it will raise error max() will always will always return the first encountered max element. If there are multiple max elements then we need a customized solution to find all the max elements.

Using max() function with Multiple arguments

We can also pass the individual elements in max function instead of any Iterable i.e.

Output:

Complete example is as follows,

Output:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,