Open In App

Python List max() Method

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Example:

Python3




#creating a list
rand = [2,3,6,1,8,4,9,0]
#printing max element
print(max(rand))


Output

9

Definition of List max() Function

max() function in Python finds and returns the largest element in that list. Lets understand it better with an Example:

Python List max() Method 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.

How to Find Maximum Element in List in Python?

To find the maximum element in Python, you can use list max() function that returns max element from the list. Let’s understand it better with an example:

Python3




#creating a list
number = [54,67,12,33,69,32]
#printing max element
print(max(number))


Output

69

Python List max() Method Example

Lets look at some examples to find max element in Python list:

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)


Output

e

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.

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) .

We have covered the definition, syntax and examples of max() function in Python. List max() method is very important for arithmetic projects and can save you a lot of time.

Hope you understood the use and functioning of max() function!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads