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 max function in python is:

# Simple Syntax
max(Iterable)

# Optional key argument
max(Iterable, key)

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

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

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.

# Python max Function Example

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

Python Tuple max 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 maximum value inside an integer tuple. Next, we are finding the maximum value inside a string tuple.

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

# Python max Function Example

# Python Tuple Max Example 
maxTuple = (12, 22, 32, 42, 52, 62, 72,82)
print("\n Original Tuple = ", maxTuple)
      
print("Maximum Value in a Tuple = ", max(maxTuple))


# Python Tuple Max Example 2
maxStringTuple = ('berry', 'orange', 'banana', 'mango')
print("\n Original String = ", maxStringTuple)
      
print("Maximum String in a Tuple = ", max(maxStringTuple))

Tuple max function output


 Original Tuple =  (12, 22, 32, 42, 52, 62, 72, 82)
Maximum Value in a Tuple =  82

 Original String =  ('berry', 'orange', 'banana', 'mango')
Maximum String in a Tuple =  orange

Python List max 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 the max function. Next, we are fining the maximum item in a string list.

# Python max Function Example

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


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

Python Dictionary max

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 the max key, then it finds the maximum key within a dictionary.
  2. The max function, along with dictionary values, finds the maximum value in dictionary values.
# Python max Function Example

# Python Dictionary Max Example 
maxDictionary = {7: 100, 2: 40, 9: 10, 5: 60, 1: 420, 3: 120}
print("\n Original Dictionary = ", maxDictionary)
      
print("Maximum Key in a Dictionary = ", max(maxDictionary.keys()))
print("Maximum Value in a Dictionary = ", max(maxDictionary.values()))

# Python Dictionary Max Example 2
maxStringDictionary = {1: 'grape', 2: 'banana', 3: 'cherry'}
print("\n Original Dictionary = ", maxStringDictionary)
      
print("Maximum Key in string Dictionary = ", max(maxStringDictionary.keys()))
print("Maximum Value in String Dictionary = ", max(maxStringDictionary.values()))

Dictionary max function output


 Original Dictionary =  {7: 100, 2: 40, 9: 10, 5: 60, 1: 420, 3: 120}
Maximum Key in a Dictionary =  9
Maximum Value in a Dictionary =  420

 Original Dictionary =  {1: 'grape', 2: 'banana', 3: 'cherry'}
Maximum Key in string Dictionary =  3
Maximum Value in String Dictionary =  grape

Python set max Example

The max function 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 maximum item (starts with the highest Alphabet) of a string set.

# Python max Function Example

# Python Set Max Example 
maxSet = {19, 49, 229, 435, 212, 502, 172, 182}
print("\n Original Set = ", maxSet)
      
print("Maximum Value in a Set = ", max(maxSet))


# Python Set Max Example 2
maxStringSet = {'cherry', 'berry', 'kiwi', 'banana', 'grape'}
print("\n Original Set = ", maxStringSet)
      
print("Maximum String in a Set = ", max(maxStringSet))

set max function output


 Original Set =  {229, 172, 49, 435, 19, 212, 502, 182}
Maximum Value in a Set =  502

 Original Set =  {'berry', 'cherry', 'kiwi', 'grape', 'banana'}
Maximum String in a Set =  kiwi

Python max key Example

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

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

# Python max Function Example

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

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

# Python Set Max Example 2
maxSet = {19, 49, 229, 435, 212, 502, 172, 182}
print("\n Original Set = ", maxSet) 
print("Maximum Value in a Set = ", max(maxSet, key = sum_of_list))

max list, and set output


 Original List =  [187, 20, 42, 212, 502, 12, 172, 182]
Maximum Value in a List =  187

 Original Set =  {229, 172, 49, 435, 19, 212, 502, 182}
Maximum Value in a Set =  229

Python maximum key Example 2

You can also use built-in functions as the key value in the max function. In this max example, we use len function as the key. Here, the max function returns a list whose length is maximum (highest number of items). Next, we declared three different lists of numeric values. Using this function, we are returning a set with maximum length.

# Python max Function Example

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

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

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

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

print("Maximum 1 = ", max(maxList1, maxSet1, key = len))
print("Maximum 2 = ", max(maxList1, maxSet2, key = len))
print("Maximum 3 = ", max(maxList1, maxSet3, key = len))
print("Maximum 4 = ", max(maxList2, maxSet1, key = len))

Python maximum keys output

Maximum List =  [212, 58, 12, 142, 192, 502, 12, 172, 182]
Maximum Set =  {229, 1200, 49, 435, 19, 212, 502, 182}
Maximum 1 =  {229, 1200, 49, 435, 19, 212, 502, 182}
Maximum 2 =  [187, 20, 42, 212]
Maximum 3 =  {20, 70, 40, 10, 30}
Maximum 4 =  [212, 58, 12, 142, 192, 502, 12, 172, 182]