Python max Function

The Python max function used to find the maximum item of a given object. Or, say, Python max function returns the highest, largest, or biggest items in a given object.

In this section, we discuss how to use this Python max function on Tuple, List, Dictionary, and Sets with practical examples. The syntax of this function is:

# Simple Syntax
max(Iterable)

# Optional key argument
max(Iterable, key) 

# Directly on arguments
max(num1, num2, num3...., numN)
max(num1, num2,......,numN, key)

Generally, the Python max function key value assigns to a default value. However, it accepts funcs. For example, key = function_name. Remember, either you can use the built-in functions like len, max, or your own custom-defined funcs.

Python max Example

In this example, we used the Python max function directly inside a print statement. The below code snippet finds the maximum Numeric Value within the given arguments.

print(max(19, 49, 229, 435, 212, 182))
print(max(212, 58, 12, 142, 192, 502, 12, 172, 182))
print(max(10, 20, 30, 40, 70))
print(max(421, 2, 102,  122))
435
502
70
421

Python max Tuple Example

This example finds the maximum item in a Tuple using the max function. It means finding the largest value in this Python tuple. First, we declared an integer tuple and finding the highest value inside an integer tuple. Next, we are finding the largest value inside a string tuple.

NOTE: Finding the largest in string tuple means it returns the word that starts with the highest Alphabet.

# Tuple Example 
maxTuple = (12, 22, 32, 42, 52, 62, 72,82)
print(max(maxTuple))

maxStringTuple = ('berry', 'orange', 'banana', 'mango')
print(max(maxStringTuple))
82

orange

Python max List Example

This max function also helps you to find the maximum list item available in a given List. Here, we declared a numeric list and found the maximum value in that list using it. Next, we are fining the largest item in a string list.

# List Example 
maxList = [187, 20, 42, 212, 502, 12, 172, 182]
print("\n Original List = ", maxList)
      
print("Maximum Value in a List = ", max(maxList))


maxStringList = ['cherry', 'berry', 'banana', 'mango']
print("\n Original String = ", maxStringList)
      
print("Maximum String in a List = ", max(maxStringList))
Python max Function 3

Python max Dictionary

The Python max function on Dictionary helps you find the maximum value in the Python Dictionary. When working with dictionary,

  1. If you use the keys function along with this, then it finds the maximum key within a dictionary.
  2. Use along with dictionary values, finds the largest value in dictionary.
# Dictionary Example 
maxDictionary = {7: 100, 2: 40, 9: 10, 5: 60, 1: 420, 3: 120}      
print("Largest Key = ", max(maxDictionary.keys()))
print("Largest Value = ", max(maxDictionary.values()))

maxStringDictionary = {1: 'grape', 2: 'banana', 3: 'cherry'}
print("Largest Key = ", max(maxStringDictionary.keys()))
print("Largest Value = ", max(maxStringDictionary.values()))
Largest Key =  9
Largest Value =  420

Largest Key =  3
Largest Value =  grape

Python max set Example

This method also returns the maximum set item from given set items. We declared an integer set and found the maximum set value in it. Next, we are finding the largest item (starts with the highest Alphabet) of a string set.

# Set Example 
maxSet = {19, 49, 229, 435, 212, 502, 172, 182}     
print(max(maxSet))

maxStringSet = {'cherry', 'berry', 'kiwi', 'banana', 'grape'}  
print(max(maxStringSet))
502

kiwi

In this example, we show how to use the key argument with this method. To demonstrate the same, we created a method called the sum of the list. It returns the sum of individual digits in a number. Please refer Sum of Digits article to understand the logic.

Next, we used this Python max function as the key. It means this will returns the List Item, whose sum of digits is largest.

def sum_of_list(Number):
    Sum = 0
    if(Number > 0):
        Reminder = Number % 10
        Sum = Sum + Reminder
        sum_of_list(Number //10)
    return Sum

maxList = [187, 20, 42, 212, 502, 12, 172, 182]
print("Maximum Value in a List = ", max(maxList, key = sum_of_list))

maxSet = {19, 49, 229, 435, 212, 502, 172, 182}
print("\nMaximum Value in a Set = ", max(maxSet, key = sum_of_list))
Maximum Value in a List =  187

Maximum Value in a Set =  229

You can also use built-in functions as the key value in this method. In this example, we use len method as the key.

Here, it returns a list whose length is maximum (highest number of items). Next, we declared three different lists of numeric values. Using this, we are returning a set with maximum length.

maxList1 = [187, 20, 42, 212]
maxList2 = [212, 58, 12, 142, 192, 502, 12, 172, 182]
maxList3 = [421, 2, 102,  122]

print("List = ", max(maxList1, maxList2, maxList3, key = len))

# Set Example 2
maxSet1 = {19, 49, 229, 435, 212, 502, 182, 1200}
maxSet2 = {9, 249, 977}
maxSet3 = {10, 20, 30, 40, 70}

print("Set = ", max(maxSet1, maxSet2, maxSet3, key = len))

print(max(maxList1, maxSet1, key = len))
print(max(maxList1, maxSet2, key = len))
print(max(maxList1, maxSet3, key = len))
print(max(maxList2, maxSet1, key = len))
List =  [212, 58, 12, 142, 192, 502, 12, 172, 182]
Set =  {229, 1200, 49, 435, 19, 212, 502, 182}

{229, 1200, 49, 435, 19, 212, 502, 182}
[187, 20, 42, 212]
{20, 70, 40, 10, 30}
[212, 58, 12, 142, 192, 502, 12, 172, 182]