Python

Python Max() Function

In Python, different operations such as adding, removing, appending, extracting, and others are performed using various modules and functions. To get the largest item from the sequences or the largest item from the multiple arguments, the inbuilt “max()” function is used in Python.

This guide presented a detailed tutorial on Python “max()” function using several examples and via the below content:

What is the “max()” Function in Python?

The “max()” function in Python is used to retrieve the largest element/item in an iterable or get the largest item/element between multiple specified parameters. The syntax of this function is shown below:

max(val1,val2,val3... ,key)

 

In the above syntax:

  • The “val1”, “val2”, and “val3” parameters specify two or more than two values that are used for comparison.
  • The “key” parameter specifies the function that is used as the criteria for the comparison.

We can also utilize the below syntax:

max(iterable,key,default)

 

In this syntax:

  • The “iterable” parameter specifies any iterable used to compare one or multiple items.
  • The “key” parameter indicates the comparison criteria function used for comparing items.
  • The “default” parameter represents the retrieved value when the iterable is empty.

Return Value

The “max()” function retrieves the largest element/item from the iterable.

Example 1: Determining the Largest Variable Between Multiple Parameters

The “max()” function is used in the below code to determine the largest variable between multiple integers, floats, and strings variables:

print('Largest Integer Variable: ', max(4, 8, 5))
print('\nLargest Float Variable: ', max(4.9, 5.8, 6.1, 2.4, 4.5))
print('\nLargest String Variable: ', max('Joseph', 'Anna', 'Lily'))

 

The above code execution displays the following output to the console:

Note: In this code, the largest string is selected based on alphabetical order.

Example 2: Determining the Largest String Variables Based on the Length

The “key” parameter can also be used to specify the criteria for comparison of specified string variables. Here in this example, the “max()” function takes the string variable and “key=len” parameter as an argument to retrieve the largest string variable based on length.

str1 = 'Joseph'
str2 = 'Anna'
str3 = 'Lily'
print('Largest String Variable: ', max(str1, str2, str3, key=len))

 

The following output is retrieved by executing the above code:

Example 3: Determining the Largest Element from a List, Dictionary, and Tuple

The “max()” function can also determine the largest element of any iterable, such as a list, tuple, or dictionary. In the following code, the “max()” function accepts a list, dictionary, and tuple as an argument to retrieve the largest element:

dict_value = {'Name': 'Joseph', 'Age': 12, 'Height': 5.5}
list_value = ['Anna', 'Joseph', 'Henry']
tuple_value = (4, 5, 6, 7, 9)
print("Largest Dictionary Element: ",max(dict_value))
print("\nLargest List Element: ",max(list_value))
print("\nLargest Tuple Element: ",max(tuple_value))

 

The below output is retrieved after executing the above code:

Example 4: Determining the Largest Element from the List of Strings Using the Inbuilt Function

In this example, the “max()” function takes the list of strings and the inbuilt len() function as the “key” parameter value as an argument and retrieves the largest element:

list_value = ['Anna', 'Joseph', 'Henry', 'Lily', 'Casemiro']
print("Largest List Element: ",max(list_value, key=len))

 

This code generates the following output:

Example 5: Determining the Largest Element from Iterable Using the Lambda Function

We can also utilize the lambda function for comparing any iterable element value based on the specific functionality. Here in this code, the “max()” function takes the tuple list and key parameters as an argument to determine the largest element. The lambda function indicates that the max() function is applied to the second element of the tuple:

tuple_list = [('Joseph', 25),('Lily', 35),('Henry', 40)]
print(max(tuple_list, key=lambda x: x[1]))

 

The above code generates the following output:

Example 6: Determining the Largest Element from an Empty Iterable

In this example, the “max()” function is applied to the empty iterable. The “max()” function takes the empty iterable as an argument and retrieves the value error. Here is an example code that generates this error:

value1 = {}
print(max(value1))

 

The above code retrieves the following output:

We can resolve this error by using the “default” parameter in the “max()” function. The default parameter value will be retrieved when the “max()” function is applied to the empty iterable. Check out the provided code:

value1 = {}
print(max(value1, default=None), '\n')
print(max(value1, default={0: 'Joseph'}))

 

The above code displays the following output:

Conclusion

In Python, the “max()” function is utilized to retrieve the largest item in an iterable or between the input-specified variables. This method can be applied to the passed multiple variables or different sequences such as lists, strings, tuples, or dictionaries to get the largest item. We can also get the largest number based on the length of the input string. This blog provided a detailed overview of the Python “max()” function using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.