The Wayback Machine - https://web.archive.org/web/20240712133823/https://www.geeksforgeeks.org/python-list-max-method/
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!



Previous Article
Next Article

Similar Reads

List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3. max() :- This function returns the maximum eleme
2 min read
Python | Min/Max value in float string list
Sometimes, while working with a Python list, we can have a problem in which we need to find min/max value in the list. But sometimes, we don't have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Let's discuss a way in which this problem
6 min read
Python | Decimal max() method
Decimal#max() : max() is a Decimal class method which compares the two Decimal values and return the max of two. Syntax: Decimal.max() Parameter: Decimal values Return: the max of two. Code #1 : Example for max() method # Python Program explaining # max() method # loading decimal library from decimal import * # Initializing a decimal value a = Deci
2 min read
Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace &amp; expandtabs())
Some of the string methods are covered in the below sets.String Methods Part- 1 String Methods Part- 2More methods are discussed in this article1. strip():- This method is used to delete all the leading and trailing characters mentioned in its argument.2. lstrip():- This method is used to delete all the leading characters mentioned in its argument.
4 min read
Python String | max()
max() is an inbuilt function in Python programming language that returns the highest alphabetical character in a string. Syntax: max(string) Parameter: max() method takes a string as a parameter Return value: Returns a character which is alphabetically the highest character in the string. Below is the Python implementation of the method max() Pytho
1 min read
Python | Pandas Series.max()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.max() function return the maximum of the underlying data in the given Series ob
3 min read
Python | Pandas dataframe.max()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.max() function returns the maximum of the values in the given object. If the input is a series, the method will return
2 min read
Python | Pandas Index.max()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.max() function returns the maximum value of the Index. The function works with both numerical as well as the string type ob
2 min read
Python | Pandas TimedeltaIndex.max
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.max() function return the maximum value of the TimedeltaIndex object or maximum along an axis. Syntax : TimedeltaI
2 min read
Use of min() and max() in Python
Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively, when we passed string or list of strings as argum
2 min read
Practice Tags :
three90RightbarBannerImg