  Table of content

## How to find maximum value in a list in Python?

A list is a built-in data type in python, that stores a collection of data items, separated by commas and enclosed within square brackets []. A list can hold data items of different data types. The list is mutable, ordered, indexed, and allows duplicate values.

For eg, list_1 = [1, 9, 4, 5] represents a list. The maximum value in list_1 is 9. This article discusses several ways to find maximum value in a list.

### Finding the maximum value using the sort() method

The sort() method sorts the original list in ascending order by default. The sort() method doesn't take any parameters and doesn't return value. A list when sorted in ascending order, the last item of the list will give the maximum value of the list.

Consider a list list_1 = [1, 44, 9, 18, 34]. list_1.sort() sorts the list_1 to [1, 9, 18, 34, 44]. list_1[-1] returns the last element of the list.

``````list_1 = [1, 44, 9, 18, 34]
print("list_1 = ", list_1)
list_1.sort()
print("sorted list = ", list_1)
print("The maximum value is : ", list_1[-1])
``````

The above code returns the maximum value of 44 as output.

``````list_1 =  [1, 44, 9, 18, 34]
sorted list =  [1, 9, 18, 34, 44]
The maximum value is :  44``````

### Finding the maximum value using the max() function

The max() is a built-in function in python, that takes a sequence(list, tuple, string, dictionary) as a parameter and returns the maximum value. The syntax for the max() function is as follows.

``````#syntax:
max(sequence)``````

Consider a list list_1 = [1, 4, 9, 18, 34]. The max() function takes list_1 as parameter and returns the maximum value 34.

``````list_1 = [1, 4, 9, 18, 34]
print("list_1 = ", list_1)
maxim = max(list_1)
print("The maximum value is : ", maxim)``````

Output

``````list_1 =  [1, 4, 9, 18, 34]
The maximum value is :  34``````

Example 2: Passing a list of strings as a parameter

When a list of strings is passed as a parameter to max() function it compares Unicode values of each character in the string and returns the string with max Unicode value. Consider a list list_1 = {"aa", "ac", "ab"}. The max() function takes list_1 as a parameter and returns the maximum value "ac".

``````list_1 = {"aa", "ac", "ab"}
print("list_1 = ", list_1)
maxm = max(list_1)
print("The maximum value is : ", maxm)
``````

The above code returns the output as

``````list_1 =  {'aa', 'ac', 'ab'}
The maximum value is :  ac``````

### Finding the maximum value using for loop

Consider a list list_1 = [1, 4, 9, 18, 34]. A variable maxim is initialized to - infinity. A for loop iterates over the list_1 and compares each value with maxim. If the value is greater than the maxim, the value is assigned to the variable maxim.

``````list_1 = [1, 4, 9, 18, 34]
print("list_1 = ", list_1)
maxim = -math.inf
for i in list_1:
if i > maxim:
maxim =i
print("The maximum value is : ", maxim)``````

The above code gives output as

``````list_1 =  [1, 4, 9, 18, 34]
The maximum value is :  34``````

The list must contain all the elements of the same data type to get the maximum value. Otherwise, all the above method raises the TypeError exception.

Example,

``````list_1 = [1, 4, 9, 18, 34, "red"]
maxim = max(list_1)
print("The maximum value is : ", maxim)``````

Output

``````maxm = max(list_1)
TypeError: '>' not supported between instances of 'str' and 'int'
``````
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions