The Python max function is used to find the maximum item of a given object. Or, say, the 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, and its syntax 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 or finds the largest value in this Python tuple. First, we declared an integer tuple and found the highest value inside an integer tuple. Next, we find the largest value inside a string tuple.
NOTE: Finding the largest string tuple means it returns the word that starts with the highest Alphabet.
# Tuple Example Tp = (12, 22, 32, 42, 52, 62, 72,82) print(max(Tp)) StrTp = ('berry', 'orange', 'banana', 'mango') print(max(StrTp))
82
orange
Python max List Example
It 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 find the largest item in a string list.
# List Example LiA = [187, 20, 42, 212, 502, 12, 172, 182] print("\n Original Object = ", LiA) print("Largest Value in this List = ", max(LiA)) StrLiA = ['cherry', 'berry', 'banana', 'mango'] print("\n Original String = ", StrLiA) print("Largest String in a List = ", max(StrLiA))
max Dictionary
The Python max function helps you find the maximum value in the Dictionary. When working with a dictionary,
- If you use the keys function along with this, then it finds the maximum key within a Dictionary.
- Use along with dictionary values to find the largest value in the 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
set max Item Example
This Python max function also returns the maximum set item from the given set items. In this example, we declared an integer set and found the maximum set value in it. Next, we find the largest item (which starts with the highest Alphabet) of a string set.
# Set Example setA = {19, 49, 229, 435, 212, 502, 172, 182} print(max(setA)) StringStA = {'cherry', 'berry', 'kiwi', 'banana', 'grape'} print(max(StringStA))
502
kiwi
In this example, we show how to use the set key argument with this Python max Function. 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 max function as the set key. It means this will return the List Item, whose sum of digits is the largest.
def sum_of_Num(Number): Sum = 0 if(Number > 0): Reminder = Number % 10 Sum = Sum + Reminder sum_of_Num(Number //10) return Sum LtA = [187, 20, 42, 212, 502, 12, 172, 182] print("List = ", max(LtA, key = sum_of_Num)) StA = {19, 49, 229, 435, 212, 502, 172, 182} print("\nSet = ", max(StA, key = sum_of_Num))
List = 187
Set = 229
You can also use built-in functions as the key value in this method. In this example, we use the len method as the key.
Here, The Python max function returns a list whose length is maximum (highest number of set items). Next, we declared three different lists of numeric values. Using this, we are returning a set with maximum length.
ls1 = [187, 20, 42, 212] ls2 = [212, 58, 12, 142, 192, 502, 12, 172, 182] ls3 = [421, 2, 102, 122] print("List = ", max(ls1, ls2, ls3, key = len)) # Set Example 2 seA1 = {19, 49, 229, 435, 212, 502, 182, 1200} seA2 = {9, 249, 977} seA3 = {10, 20, 30, 40, 70} print("Set = ", max(seA1, seA2, seA3, key = len)) print(max(ls1, seA1, key = len)) print(max(ls1, seA2, key = len)) print(max(ls1, seA3, key = len)) print(max(ls2, seA1, 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]