Description
Python list method max returns the elements from the list with maximum value.
Syntax
Following is the syntax for max[] method −
max[list]
Parameters
list − This is a list from which max valued element to be returned.
Return Value
This method returns the elements from the list with maximum value.
Example
The following example shows the usage of max[] method.
#!/usr/bin/python list1, list2 = [123, 'xyz', 'zara', 'abc'], [456, 700, 200] print "Max value element : ", max[list1] print "Max value element : ", max[list2]
When we run above program, it produces following result −
Max value element : zara Max value element : 700
python_lists.htm
Contents
- Introduction
- Syntax
- Example 1: Get Maximum Value of Numpy Array
- Example 2: Find Max value of Numpy Array with Float Values
- Summary
Given a numpy array, you can find the maximum value of all the elements in the array.
To get the maximum value of a Numpy Array, you can use numpy function numpy.max[]
function.
Syntax
The syntax of max[] function as given below.
max_value = numpy.max[arr]
Pass the numpy array as argument to numpy.max[], and this function shall return the maximum value.
Example 1: Get Maximum Value of Numpy Array
In this example, we will take a numpy array with random numbers and then find the maximum of the array using numpy.max[] function.
Python Program
import numpy as np
arr = np.random.randint[10, size=[4,5]]
print[arr]
#find maximum value
max_value = np.max[arr]
print['Maximum value of the array is',max_value]
Run
Output
[[3 2 2 2 2]
[5 7 0 4 5]
[8 1 4 8 4]
[2 0 7 2 1]]
Maximum value of the array is 8
Example 2: Find Max value of Numpy Array with Float Values
In the following example, we will take a numpy array with random float values and then find the maximum of the array using max[] function.
Python Program
import numpy as np
arr = np.random.rand[6].reshape[2,3]
print[arr]
#find maximum value
max_value = np.max[arr]
print['Maximum value of the array is',max_value]
Run
Output
[[0.79794919 0.8307877 0.83587084]
[0.44121629 0.69983604 0.30791677]]
Maximum value of the array is 0.8358708362776799
Summary
In this Numpy Tutorial of Python Examples, we learned how to find the maximum value of Numpy Array using max[] built-in function, with the help of well detailed examples.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given an array, find the largest element in it.
Input : arr[] = {10, 20, 4} Output : 20 Input : arr[] = {20, 10, 20, 4, 100} Output : 100
Approach 1:
Python3
def
largest[arr, n]:
max
=
arr[
0
]
for
i
in
range
[
1
, n]:
if
arr[i] >
max
:
max
=
arr[i]
return
max
arr
=
[
10
,
324
,
45
,
90
,
9808
]
n
=
len
[arr]
Ans
=
largest[arr, n]
print
[
"Largest in given array "
, Ans]
Output
Largest in given array 9808
Time Complexity: O[N]
Auxiliary Space: O[1]
Approach 2 [Using built-in function max[]]:
Here we will use the inbuilt method max[] to find the maximum of the array. Below is the implementation of the approach.
Python3
def
largest[arr, n]:
ans
=
max
[arr]
return
ans;
if
__name__
=
=
'__main__'
:
arr
=
[
10
,
324
,
45
,
90
,
9808
]
n
=
len
[arr]
print
[
"Largest in given array "
, largest[arr, n]]
Output
Largest in given array 9808
Approach 3 [Using sort[] function]:
Here we use the sort[] function to sort the array. The largest element will be the last element of the sorted array. Below is the implementation of the approach.
Python3
def
largest[arr, n]:
arr.sort[]
return
arr[n
-
1
]
arr
=
[
10
,
324
,
45
,
90
,
9808
]
n
=
len
[arr]
Ans
=
largest[arr, n]
print
[
"Largest in given array "
, Ans]
Output
Largest in given array 9808
Please refer complete article on Program to find largest element in an array for more details!