Last Updated : 21 Aug, 2024
Comments
Improve
Python list max() function returns the maximum value present in the list.
Example:
#creating a listrand = [2,3,6,1,8,4,9,0]#printing max elementprint(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:
#creating a listnumber = [54,67,12,33,69,32]#printing max elementprint(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:
# 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.
# 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:
# 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.
# 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!
Python List max() Method – FAQs
What Does max() Do in Python?
The
max()
function in Python returns the largest item in an iterable (like a list or tuple) or the largest of two or more arguments. If the provided iterable is empty, it raises aValueError
. You can also pass a key function tomax()
to specify a method of determining the order.Example:
numbers = [10, 20, 30, 40, 50]
result = max(numbers)
print(result) # Output: 50
How to Get Max Value from List in Python Without max Function?
To find the maximum value in a list without using the
max()
function, you can use a simple loop to iterate through the list and keep track of the largest number.Example:
numbers = [10, 20, 30, 40, 50]
largest = numbers[0] # Start by assuming the first number is the largestfor number in numbers:
if number > largest:
largest = numberprint(largest) # Output: 50
How to Get the Max of a List of Lists in Python?
To find the maximum value in a list of lists, you can use a nested approach with the
max()
function. You first find the maximum of each sublist and then find the maximum of those maxima.Example:
lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
max_value = max(max(sublist) for sublist in lists)
print(max_value) # Output: 9
What is Max in NumPy?
In NumPy,
max()
is a function used to compute the maximum value of an array or along a specified axis. The function is highly optimized for performance and can handle very large arrays efficiently compared to native Python loops.Example:
import numpy as nparray = np.array([1, 2, 3, 4, 5])
max_value = np.max(array)
print(max_value) # Output: 5
What are the MIN() and MAX() Functions in Python? What Do They Do?
In Python:
- MIN(): The
min()
function returns the smallest item in an iterable or the smallest of two or more arguments. Likemax()
, it can also take a key function to determine how to compare the elements.- MAX(): The
max()
function, as discussed, returns the largest item in an iterable or the largest of two or more arguments.Both functions are useful for finding the extremities in sequences of values and can be customized with a
key
parameter to specify how elements are compared.Example using both min() and max():
numbers = [10, 20, 30, 40, 50]
print(min(numbers)) # Output: 10
print(max(numbers)) # Output: 50These functions are essential tools in data analysis, optimization problems, and any scenario where you need to quickly assess the range or bounds of a set of values in Python
Previous Article
Python List index()
Next Article
Python min() Function