Python : max() function explained with examples

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,

max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
  • 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.

# List of numbers
listOfNum = [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]

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.

# Find maximum value in a list
maxValue = max(listOfNum)

print('Maximum value in List : ', maxValue)

Output:

Maximum value in List :  35

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

Suppose we have a string i.e.

sampleStr = 'this is sample string'

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.

# Find character with max ASCII value in string
maxValue = max(sampleStr)

print('Character with max ASCII value : ', maxValue)

Output:

Character with max ASCII value :  t

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.

listOfStr = ['hi', 'this', 'is', 'a', 'small', 'string', 'with', 'msg']

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.

# Get string with max value in list of string based on alphabetical
maxValue = max(listOfStr)

print(maxValue)

Output:

with

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.

listOfStr = ['hi', 'this', 'is', 'a', 'small', 'text', 'with', 'msg']

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.

# Get the string with maximum size in this list of string
maxValue = max(listOfStr, key=lambda x: len(x))

print('Max Length String in List of Strings : ', maxValue)

Output:

Max Length String in List of Strings :  small

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

sampleDict = {'Ritika': 5, 'Sam': 27, 'John': 10, 'Sachin': 14, 'Mark': 19}

# Get Item with max value in dictionary
maxValue = max(sampleDict.items(), key=lambda x: x[1])

print('Item with max Value in Dictionary : ', maxValue)

Output:

Item with max Value in Dictionary :  ('Sam', 27)

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.

# Find maximum value in a given numbers
maxValue = max(22, 34, 11, 33, 10, 35, 19, 33, 9, 10)

print('Maximum value : ', maxValue)

Output:

Maximum value :  35

Complete example is as follows,

def main():
    print('*** Find maximum value in a list using max() ***')

    # List of numbers
    listOfNum = [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]

    print('List Contents : ', listOfNum)

    # Find maximum value in a list
    maxValue = max(listOfNum)

    print('Maximum value in List : ', maxValue)

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

    sampleStr = 'this is sample string'

    # Find character with max ASCII value in string
    maxValue = max(sampleStr)

    print('Character with max ASCII value : ', maxValue)

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

    listOfStr = ['hi', 'this', 'is', 'a', 'small', 'string', 'with', 'msg']

    # Get string with max value in list of string based on alphabetical
    maxValue = max(listOfStr)

    print(maxValue)

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

    print('** Find max length string from list of Strings using max() **')

    listOfStr = ['hi', 'this', 'is', 'a', 'small', 'text', 'with', 'msg']

    # Get the string with maximum size in this list of string
    maxValue = max(listOfStr, key=lambda x: len(x))

    print('Max Length String in List of Strings : ', maxValue)

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

    sampleDict = {'Ritika': 5, 'Sam': 27, 'John': 10, 'Sachin': 14, 'Mark': 19}

    # Get Item with max value in dictionary
    maxValue = max(sampleDict.items(), key=lambda x: x[1])

    print('Item with max Value in Dictionary : ', maxValue)

    print('*** Using max() function with Multiple arguments ***')

    # Find maximum value in a given numbers
    maxValue = max(22, 34, 11, 33, 10, 35, 19, 33, 9, 10)

    print('Maximum value : ', maxValue)

if __name__ == '__main__':
    main()

Output:

*** Find maximum value in a list using max() ***
List Contents :  [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]
Maximum value in List :  35
*** Find the character with max value in a String using max() ***
Character with max ASCII value :  t
*** Find max string from list of Strings based on alphabetical order using max() ***
with
*** Using max() function with Iterable & Custom comparater / key function ***
** Find max length string from list of Strings using max() **
Max Length String in List of Strings :  small
** Find item in a dictionary with maximum value using max() **
Item with max Value in Dictionary :  ('Sam', 27)
*** Using max() function with Multiple arguments ***
Maximum value :  35

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top