Online Tutorials & Training Materials | STechies.com
Register Login

Use of Max() Function in Python

|| 0

Use of Max() Function in Python
Stechies

In python, max() function returns the largest element from an iterable or maximum from multiple arguments.

In python we can use this max function with list/array, tuple, sets & dictionary.

If you want to find the smallest element, use min() method.

Syntax :

max(a,b,c,..)

max(iterable, *[, key, default])

Parameters :

x,y,z… (required): multiple items to compare

Iterable : (required) string, list, tuple etc

Return Value :

Returns the largest from all the arguments.

Exceptions :

Returns Error when conflict with arguments passed.

key (optional): Key is the name of the function from which an argument or iterable is passed and the comparison will be done on the basis of the value return by this key function.

Example: Python code to explain max() function

# Python code to explain max() function

# Find maximum of integers
print('The Max is: ',max(1, 23, 3, 9))

# Find maximum character in arguments
print('The Max is: ',max("Mike", "John", "Vicky"))

Output:

The Max is: 23
The Max is: Vicky

max () Function with Array/List

# Python code to explain max() function with list/array

# Declare integer list
listInt = [1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2]

# Declare string list
listText = ['Red', 'Orance', 'Blue', 'Green', 'Black']

# Find maximum number in list
print('The Maximum number in list: ', max(listInt))

# Find maximum character in list without key function
print('The Maximum: ', max(listText))

# Find maximum character in list with key function
print('The Maximum length of element in list: ', max(listText, key=len))

Output:

The Maximum number in list: 317
The Maximum: Red
The Maximum length of element in list: Orance

max () Function with Tuple

# Python code to explain max() function with tuple

# Declare integer tuple
listtuple = (1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2)

# Declare string tuple
listtuple = ('Red', 'Orance', 'Blue', 'Green', 'Black')

# Find maximum number in tuple
print('The Maximum number in tuple: ', max(listtuple))

# Find maximum character in tuple without key function
print('The Maximum: ', max(listtuple))

# Find maximum character in tuple with key function
print('The Maximum length of element in tuple: ', max(listtuple, key=len))

Output:

The Maximum number in tuple: Red
The Maximum: Red
The Maximum length of element in tuple: Orance

max () Function with Dictionary

# Python code to explain max() function with dictionary

# Declare dictionary
listdis = {1:"Red", 3:"Orance", 2:"Blue", 4:"Green", 7:"Black"}

# Find maximum in dictionary
print('The Maximum number in dictionary: ', max(listdis))

Output:

The Maximum number in dictionary: 7

Errors with max function

TypeError: '>' not supported between instances of 'str' and 'int'

# Python code to explain max() function

# Find maximum of integers with one string
print('The Max is: ',max(1, 'stechies', 3, 9))

Output:

Traceback (most recent call last):
File "max.py", line 4, in <module>
print('The Max is: ',max(1, 'stechies', 3, 9))
TypeError: '>' not supported between instances of 'str' and 'int'

In the above example, we are trying to find the maximum number between in string and integer.

Correct code:

print('The Max is: ',max(1, 4, 3, 9))

TypeError: object of type 'int' has no len()

Example:

# Declare integer list
listInt = [1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2]

# Find maximum in integer list with key function
print('The Maximum length of element in list: ', max(listInt, key=len))

Output:

Traceback (most recent call last):
File "max.py", line 5, in <module>
print('The Maximum length of element in list: ', max(listInt, key=len))
TypeError: object of type 'int' has no len()

In the above example of code, we are trying to obtain the length of an integer value.

Correct code:

# Declare integer list
listInt = [1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2]

# Find maximum in integer list with key function
print('The Maximum length of element in list: ', max(listInt))

ValueError: max() arg is an empty sequence

Code:

# Declare integer list
listInt = []

# Find maximum in integer list with key function
print('The Maximum length of element in list: ', max(listInt))

Output:

Traceback (most recent call last):
File "max.py", line 5, in <module>
print('The Maximum length of element in list: ', max(listInt))
ValueError: max() arg is an empty sequence

In the above example, we have passed an empty list

Correct code:

# Declare integer list
listInt = [1,3,6,3,6]

# Find maximum in integer list with key function
print('The Maximum length of element in list: ', max(listInt))