Python max List Function

Python max List function is used to return the maximum value. In this section, we discuss how to use this Python List maximum function. The syntax of the max function is

max(listname)

Python max List Function Example 1

The python max function returns the maximum value in a List. The code below finds the maximum value in an integer.

intList = [10, 150, 160, 180, 120, 105]
 
print("The Maximum Value in this List is : ", max(intList))
The Maximum Value in this List is :  180

The Python List max function finds the maximum value.

intLi = [50, 10, 150, 20, 205, 500, 7, 25, 175]
print(intLi)

maximum = max(intLi)

print("\nThe Maximum item = ", maximum)
[50, 10, 150, 20, 205, 500, 7, 25, 175]

The Maximum item =  500

In this example, we declared a string list. Next, we used the max function to return Maximum value in this. Here, the max function uses the alphabetical order to find the maximum value.

Please refer to List article and methods article to understand everything about them in Python.


Fruits = ['Orange', 'Banana', 'Watermelon', 'Kiwi', 'Grape', 'Blackberry']
 
print("The Maximum Value : ", max(Fruits))
The Maximum Value :  Watermelon

Python max of List Example 3

This list max program is the same as the first example. However, this time, we allowed the user to enter the length. Next, we used For Loop to add numbers to it.

intList = []

number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intList.append(value)
 
print("The Maximum Value in this List is : ", max(intList))
Python Max List Function 3

This program allows users to enter their own string or words and then finds the maximum string

strLi = []

number = int(input("Please enter the Total : "))
for i in range(1, number + 1):
    item = input("Please enter the Value of %d Element : " %i)
    strLi.append(item)
 
print("The Maximum Value is : ", max(strLi))
Please enter the Total : 3
Please enter the Value of 1 Element : Kiwi
Please enter the Value of 2 Element : Orange
Please enter the Value of 3 Element : Apple
The Maximum Value is :  Orange

Let me use this max function on Mixed List.

MixedLi = ['apple',  1, 5, 'Kiwi', 'Mango']
 
print("The Maximum Value : ", max(MixedLi))

As you can see, it is throwing an error because it can’t apply < operation on string and int.

Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 3, in <module>
    print("The Maximum Value : ", max(MixedLi))
TypeError: '>' not supported between instances of 'int' and 'str'

This time, we used Python max function on the Nested list. Here, the max function uses the first value in each nested list, and return a maximum value from that nested.

MixedList = [[1, 2], [22, 3], [4, 5]]
 
print("The Maximum Value : ", max(MixedList))
The Maximum Value :  [22, 3]