Python Tutorial Python Advanced Python References Python Libraries

Python max() Function



The Python max() function returns largest element in an iterable. An iterable object can be any data structure like list, tuple, set, string, dictionary and range iterable.

Syntax

#to compare one or more elements
max(n1, n2, n3, .....)

#to compare all elements of an iterable
max(iterable)

Parameters

iterable Required. iterable object like list, tuple, set, string , dictionary and range() etc.

Example: max() function applied on numerical iterable

In the example below, max() function is used to find out the largest element in a given iterable.

MyList = [10, 5, 15, 20, 25, 30]
print(max(MyList))

MyRange = range(10, -5, -1)
print(max(MyRange))

x = max(10, 0, 14, 18, 20, 30)
print(x)

The output of the above code will be:

30
10
30

Example: max() function applied on a string iterable

When max() function is applied on a string iterable, it returns largest value, ordered alphabetically.

MyList = ['Marry', 'Sam', 'John', 'Joe']
print(max(MyList))

The output of the above code will be:

Sam

❮ Python Built-in Functions

5