Sunday, November 17, 2024
Google search engine
HomeLanguagesPython List max() Method

Python List max() Method

Python list max() function returns the maximum value present in the list.

Python List max() Method Syntax:

Syntax: max(listname)

Parameter:

  • listname : Name of list in which we have to find the maximum value.

Returns : It return the maximum value present in the list.

Python List max() Method Example

Example 1:

In this example, we are going to find the maximum integer value in the list. For that, we have to make a list of some random integers and then use the Python list max() function to find the maximum value in the list. Let’s implement this using code.

Code:

Python3




# Declaring a list with random integers.
list1 = [4, -4, 8, -9, 1]
 
# Store maximum value in a variable
# using Python list max() function.
maxValue = max(list1)
 
# Printing value stored in maxValue.
print(maxValue)


Output:

8

Example 2:

In this example, we are going to find the maximum value from the list of characters. This is done by following same procedure as in example 1.

Python3




# Declaring a list with random integers.
list1 = ['a', '$', 'e', 'E']
 
# Store maximum value in a variable
# using Python list max() function.
maxValue = max(list1)
 
# Printing value stored in maxValue.
print(maxValue)


In this case of character values the max value is determined on the basis of their ASCII values. For example ASCII value of ‘e’ is 101 and ‘E’ is 69 therefore ‘e’ is larger.

Output:

e

Example 3:

In this example, we are going to try to find the maximum value from the list of mixed values such as integers and strings.

Code:

Python3




# Declaring a list with mixed values.
list1 = ['a', '$', 'e', 3, 16]
 
# Store maximum value in a variable
# using Python list max() function.
maxValue = max(list1)
 
# Printing value stored in maxValue.
print(maxValue)


As we can see in the output this code gives the error because finding the maximum value between integers and string is not supported by Python list max() function.

Output:

Traceback (most recent call last):
  File "/home/b98ffbce53474d196bfb2d69e59d0873.py", line 6, in <module>
    maxValue = max(lis)
TypeError: '>' not supported between instances of 'int' and 'str'

Below is the solution of above example no 3-

Solution :

We can find the maximum value between integers and string to convert integer into char but according to ASCII table only.

Python3




# Declaring a list with mixed values.
list1 = ['!', '$', '/', '3', '61']
 
# Store maximum value in a variable
# using Python list max() function.
maxValue = max(list1)
 
# Printing value stored in maxValue.
print(maxValue)


Output –

61 

Note- integers lie between 48 to 57 from 1 to 9 in ascii table and it will consider only first char
of a string. Let if in above example there will be 16 instead of 61 then output will be 3 because 
it will consider first char which is 1 in 16.

Complexity- 

The time complexity of max() function in python is O(n) .

RELATED ARTICLES

Most Popular

Recent Comments