Python max() Function

Summary: in this tutorial, you’ll learn how to use the Python max() function to return the maximum item of an iterable or maximum argument of two or more arguments.

Introduction to the Python max() function

The max() function returns the maximum argument of two or more arguments:

max(arg1,arg2,*args[,key])Code language: Python (python)

In this syntax:

  • arg1, arg2, .. are the input arguments.
  • key is an optional keyword-only parameter that specifies a key for comparing the arguments. It is similar to the key in the sort() function.

The max() function also accepts an iterable and returns the maximum item of the iterable:

max(iterable, *[, key, default])Code language: Python (python)

In this syntax:

  • iterable is the input iterable.
  • key is an optional keyword-only argument that specifies the key for comparing the items of the iterable.
  • default is an optional keyword-only argument that specifies a value that the max() returns when the iterable is empty. If the iterable is empty and the default argument is not available, the max() function raises a ValueError exception.

If multiple items are maximal, the max() function will return the first one it encounters.

Python max() function examples

Let’s take some examples of using the Python max() function.

1) Using the Python max() function to find the maximum argument of multiple arguments

The following example uses the max() function to get the largest number:

result = max(1, 2, 3)
print(result) # 3Code language: Python (python)

If you pass multiple string arguments, the max() function will return the maximal string based on alphabetical order:

result = max('John', 'Alice', 'Jane')
print(result)  # JohnCode language: Python (python)

The following example shows how to use the max() function with user-defined objects:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def __str__(self):
        return f'Name:{self.name}, salary:{self.salary:,}'


result = max(
    Employee('John', 210_000),
    Employee('Alice', 220_000),
    Employee('Jane', 200_000),
    key=lambda p: p.salary
)

print(result)
Code language: Python (python)

Output:

Name:Alice, salary: 220,000Code language: Python (python)

How it works.

First, define the Employee class with two attributes, name and salary:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def __str__(self):
        return f'Name:{self.name}, salary:{self.salary:,}'Code language: Python (python)

Second, use the max() function to find the employee with the highest salary:

result = max(
    Employee('John', 210_000),
    Employee('Alice', 220_000),
    Employee('Jane', 200_000),
    key=lambda p: p.salary
)Code language: Python (python)

Besides passing the Employee objects, we also specify the salary as the key for object comparisons.

2) Using the Python max() function to find the maximum item of an iterable

The following example uses the max() function to find the largest number of a list:

scores = [1, 5, 3, 2]
max_score = max(scores)
print(max_score)  # 5Code language: Python (python)

Similarly, this example uses the max() function to find the person with the maximal name based on alphabetical order:

names = ['John', 'Alice', 'Jane']
name = max(names)
print(name)  # JohnCode language: Python (python)

This example shows how to use the max() function to find the employee with the highest salary from a list of employees:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def __str__(self):
        return f'Name:{self.name}, salary:{self.salary:,}'


employees = [
    Employee('John', 210_000),
    Employee('Alice', 220_000),
    Employee('Jane', 200_000),
]

result = max(employees, key=lambda p: p.salary)

print(result)
Code language: Python (python)

Output:

Name:Alice, salary:220,000Code language: Python (python)

In this example, we pass a list of Employee objects and use the max() function to find the employee who has the highest salary.

Summary

  • Use the max() function to find the maximum item of an iterable or the maximum argument of arguments.
  • Use the key parameter to specify the key to compare the objects.
Did you find this tutorial helpful ?