Online Tutorials & Training Materials | STechies.com
Register Login

Use of Max() Function in Python

12 Sep 2019 2:21 pm || 0

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.

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

# 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

Explanation

According to the output, the max() method prints out the largest number among 1,23,3 and 9, that is 23. Then in the next print() statement, max() checks which greatest string. As “Vicky” appears last according to the alphabetical order, it is printed out.       

1) max() Function with Array/List

Example

# 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

Explanation

In this code, we can see that the max() method checks the largest number among the ones in the list and prints it out. Then, the method checks the largest string as per their alphabetical order and prints it. The last statement evaluates the same strings but according to their length, as specified by the key value.    

2) max() Function with Tuple

Example

# 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

Explanation

At first, the max() checks the tuple containing strings and prints out the largest string according to alphabetical order. The next print() statement also works the same way. So both their outputs are the same. The last print() statement displays the largest string according to the character length, and hence the result is “Orange”

3) max() Function with Dictionary

Example

# 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

Explanation

In simple terms, in a dictionary that has a key-value pair such as 7: "Black", the max() method will only check the key while determining the largest among other key-value pairs. So, here largest key value is 7 among the rest. Therefore, it is selected as the maximum number in the dictionary.

Conclusion

The max() method can be very effective when comparing the values of the elements in large lists. But you have to be careful while using the key=len argument with the method. As mentioned earlier, the argument can cause some errors. So, check whether the elements are all integers or all of them must be strings. Make sure that the elements are homogeneous while using the max() method.