Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

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 max method 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))
Python max Function 1

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))
Python max Function 2

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()))
Python max Function 4

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))
Python max Function 5

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)
Python max Function 6

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 max Function 7

Filed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy