Register Login

Use of Max() Function in Python

Updated Jun 14, 2020

What is Max() Function in Python?

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

Here, the first print statement prints out the maximum value among the tuple having numbers (1, 23, 3, 9). The next print statement displays the greatest string according to their alphabetical order among ("Mike", "John", "Vicky"). As a result, the greatest number 23 is printed out. Then the string “Vicky” is printed out in the output.      

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 program, a variable called listInt is initialized with the integer elements [1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2]. Another variable called listText is initialized with the elements ['Red', 'Orance', 'Blue', 'Green', 'Black']. The print() method in the next line prints out the biggest element among the list of numbers in the variable listInt using the max() method. The maximum string according to the alphabetical order is assigned to listText. It is printed in the subsequent line using a max() method and print() method.

The last line of the code is print('The Maximum length of element in list: ', max(listText, key=len)). This statement displays the string element having the maximum number of characters. The key argument is passed with the len value, specifying the length of the string.

After program execution, the first print statement prints out the highest number 137. Next, the string “Red” is printed. This is because the max() method determines the maximum string value according to its alphabetical order. The last print statement prints out the string “Orange”. This happens because the key=len is given, so the max() method determines the largest string according to its length.   

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

Here a variable listtuple is initialized with the tuple values (1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2). Then a string tuple ('Red', 'Orange', 'Blue', 'Green', 'Black') is assigned to the variable listtuple. The next two print statements print out the maximum tuple value among the elements.

The last statement is print('The Maximum length of element in tuple: ', max(listtuple, key=len)). This prints the largest tuple element along the strings in the listtuple, based on the key=len. This means the string with the maximum length will be printed.

So, as per the output, the largest string “Red” is printed first. Then the same string is printed in the next line. The last statement prints the string “Orange” as it has the maximum length among the other strings.

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 this program, a dictionary called listdis is declared with the values {1:"Red", 3:"Orange", 2:"Blue", 4:"Green", 7:"Black"}. The next line prints the maximum value in the dictionary using the print() method. The max() method considers the keys in a dictionary while determining the maximum value in a dictionary. As the values are not considered by the method, the key 7 is the final output.

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.


×