Python Max() Function with Examples

Posted in

Python Max() Function with Examples
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    Python has many built-in functions that can perform specific tasks. max() is one of the widely used python functions which returns the largest item from an iterable object. Generally, max() return the largest element according to the lexicographical order, but it can also be customized using the key keyword.

    Example

    print(max(1,2,3,4,5))
    

    Output

    5

    Python max() function parameters

    We can pass 4 types of parameters in the max() function:

    • iterable objects such as string, tuples, list, set, dictionary, etc.
    • * iterables multiple iterable objects, such as multiple strings.
    • key parameter is used to customize the default comparison of the max() method.
    • default provides an alternative default value if the iterable object is empty.

    Note: In the max() function, an iterable object must be passed as a parameter, and object elements should be of a similar data type.

    max(iterable) Single Iterable Parameter

    Usually, the max() function is used to find the largest element from an iterable object. It uses the element's value and ASCII code to find the largest value.

    Example

    #max() function on list 
    >>> my_list=[1,2,3,4,5.9,70,800]
    >>> max(my_list)
    800
    
    # max() function on string
    >>> string= "techgeekbuzz"
    >>> max(string)
    'z'
    
    # max() function on tuple
    >>> tup=('t','g','b')
    >>> max(tup)
    't'
    
    # max() function on dictionary
    >>> my_dict={1:"Hello",2:"TechGeekBuzz"}
    >>> max(my_dict)
    2

    Note: in dictionary the max() function find the largest key value.

    >>>my_list=[1,"2",3,"4"]
    >>> max(my_list)
    
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: '>' not supported between instances of 'str' and 'int'

    If the iterable has different data type elements, then the max() function returns an error.

    max(*iterables) Multiple parameters

    If we pass multiple iterable objects in the max() function then it returns the largest iterable object based on its lexicographical order. For instance, "tgb" is larger than "techgeekbuzz" based on the lexicographical or dictionary-based sorting method.

    Example

    >>> max('tgb','techgeekbuzz') 'tgb'
    
    

    Behind the code

    tgb compare with techgeekbuzz t == t g > e so tgb is greater than techgeekbuzz

    >>> max([100,200,300],[400])
    [400]

    Behind the code

    [100,200,300] compare with [400] 100 < 400 so [400] is greater than [100,200,300]

    key parameter

    By default, the max() function returns the largest value based on the iterable or element lexicographical order or ASCII code. But it can be customized using the key parameter. For instance, we can use the key keyword and customize the max() function to return the largest value based on iterable length.

    Example

    #default max() function
    >>> max([100,200,300],[400])
    [400]
    
    # find the max iterable based on iterable length 
    >>> max([100,200,300],[400], key=len)
    [100, 200, 300]

    default parameter

    If the iterable object is empty then the max() function throw the ValueError error.

    >>>max([])
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: max() arg is an empty sequence

    To avoid this ValueError for an empty iterable object we use the default keyword. The default keyword specifies a default value to show if the iterable is empty.

    >>> max([],default=0)
    0

    Summary

    • max() is the built-in python function that accepts iterable objects and returns the largest value.
    • The iterable object elements should be of a similar data type.
    • With the key keyword, we can customize the comparison base of the max() function.
    • Similar to the max() function we have the min() function, which returns the minimum value from an iterable object.

    People are also reading:

    Leave a Comment on this Post

    0 Comments