Python max() Function Tutorial

In this section, we will learn what the max() function is and how to use it in Python.

max() Function in Python

The Python max() function is used to get the maximum element of a sequence of values or the maximum element of an iterable object.

Python max() Function Syntax:

max(arg1, arg2, *args[, key])

Python max() Function Parameters

The max function takes three types of arguments:

`arg1,arg2,argN`: first of all, we can pass a sequence of values of the same type (for example a bunch of numbers or a bunch of string values etc.) as the argument of this function and it will calculate the maximum value in that sequence and return it as a result.

Note: if we passed a sequence of elements as the arguments of this function (for example, a set of numbers) then all the elements in the sequence should be of the same data type. Otherwise we will get an error. There’s an exception to this, however, and it is explained in just a second.

`iterableObject`: Other than a sequence of values, we can pass an iterable object (like a list, dictionary, set, or tuple) as the argument of the function and get the maximum value as a result. Note that the elements of the target iterable object should all be of the same type! Otherwise we will get an error. Note that there’s an exception to this, and it is explained in just a second.

`key`: The `max` function is capable of calculating the maximum values for basic data types like `numbers` and `strings`. But when it comes to complicated objects like a dictionary, for example, this function doesn’t know what to do and based on what value it should calculate the maximum element. So for this reason, the function provided another parameter called `key`. This parameter takes a reference to a function and will call the function per each element (the sequence of values or the elements of the iterable object that we pass to the function). Within the body of the target function, we need to return a basic data type value per each incoming element so that the `max` function can use those returned values and then calculate the maximum element to get the final result.

Also, using the `key` function allows us to pass an iterable object or a sequence of values that has elements with different data types. But remember, it is now our responsibility to make sure the returned values of the `key` function are of the same data type.

Note: the `key` function is optional.

Python max() Function Return Value

The return value of this function is the maximum element of the target iterable object or the sequence of elements we’ve passed to the function.

Example: getting the largest number

result = max (1,2,4,7,34,3,2,645,64,342,34,34645,34,523,34,332,34,364,5634,5234,2343,4645,342)

print (result)

Output:

34645

Example: python max of list

list1 = [1,2,3,4,"five"]


def func(element):
    if type(element) ==str:
        return 10 
    else:
        return element


result = max(list1, key = func)
print (result)

Output:

five

Example: the largest string in a list

list1 = ["Jack","Elon","Ellen","Omid"]

result = max(list1)

print (result)

Output:

Omid

Example: python max function with dictionaries

list1 = [
    {"name":"Omid","age":5000},
    {"name":"John","age": 7000},
    {"name":"Elon","age":400},
    {"name":"Ellen", "age":6000}
]


def func(element):
    return element["age"]


result = max(list1 , key = func)
print (result)

Output:

{'name': 'John', 'age': 7000}
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies