You'll have to test for the type, or use exception handling, to switch approaches:
def find_max(a):
if isinstance(a, list):
return max(list)
return a.values.max()
or
def find_max(a):
try:
return a.values.max()
except AttributeError:
# assume a Python sequence
return max(a)
Which one you pick depends on a few factors:
- Do you need to support other iterables, like tuples or sets or generators?
- What is the more common case; the
try...exceptcase is faster if the majority of calls pass in a Pandas series.
Another option is to use np.max(), which works on either type:
import numpy as np
def find_max(a):
return np.max(a)
Note that there is no need to use the .values attribute here!
Top answer 1 of 3
6
You'll have to test for the type, or use exception handling, to switch approaches:
def find_max(a):
if isinstance(a, list):
return max(list)
return a.values.max()
or
def find_max(a):
try:
return a.values.max()
except AttributeError:
# assume a Python sequence
return max(a)
Which one you pick depends on a few factors:
- Do you need to support other iterables, like tuples or sets or generators?
- What is the more common case; the
try...exceptcase is faster if the majority of calls pass in a Pandas series.
Another option is to use np.max(), which works on either type:
import numpy as np
def find_max(a):
return np.max(a)
Note that there is no need to use the .values attribute here!
2 of 3
5
def find_max(a):
if isinstance(a, list):
return max(a)
elif isinstance(a, pd.DataFrame):
return a.values.max()
else:
print("No max method found for the input type")
Real Python
realpython.com › numpy-max-maximum
NumPy's max() and maximum(): Find Extreme Values in Arrays – Real Python
January 18, 2025 - In this section, you’ll become familiar with np.max(), a versatile tool for finding maximum values in various circumstances. Note: NumPy has both a package-level function and an ndarray method named max(). They work in the same way, though the package function np.max() requires the target array name as its first parameter. In what follows, you’ll be using the function and the method interchangeably. Python also has a built-in max() function that can calculate maximum values of iterables.
Videos
03:39
NumPy np.max() Tutorial: Find Maximum Values in Arrays | Python ...
02:13
Max and Min of an array | Python Exercises for Beginners - Exercise ...
08:08
Frequently Asked Python Program 6: Find Maximum & Minimum Elements ...
04:24
Calculate Max & Min of NumPy Array in Python (Example) | np.max ...
05:06
Array FindMax: Find the maximum value in an array - YouTube
06:28
How to find the maximum value in NumPy Python | Python NumPy max ...
W3Schools
w3schools.com › python › ref_func_max.asp
Python max() Function
Python Examples Python Compiler ... Python Certificate Python Training ... The max() function returns the item with the highest value, or the item with the highest value in an iterable....
Tutorialspoint
tutorialspoint.com › python › list_max.htm
Python List max() Method
print("Max value element : ", str(max(list1))) TypeError: '>' not supported between instances of 'str' and 'int'
Educative
educative.io › answers › how-to-find-the-largest-element-in-an-array-in-python
How to find the largest element in an array in Python
If it is, then assign the value in i to the variable largest. After the loop ends, largest will store the largest element in the list. ... We will use Python’s built-in function max() to solve this problem.
Python Examples
pythonexamples.org › python-numpy-maximum-value-of-array
Python Program - Maximum Value of Numpy Array - numpy.max()
To get the maximum value of a NumPy Array, you can use numpy.max() function.
NumPy
numpy.org › doc › stable › reference › generated › numpy.amax.html
numpy.amax — NumPy v2.4 Manual
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)[source]# Return the maximum of an array or maximum along an axis.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.max.html
numpy.max — NumPy v2.2 Manual
Maximum of a. If axis is None, the result is a scalar value. If axis is an int, the result is an array of dimension a.ndim - 1.
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.04-computation-on-arrays-aggregates.html
Aggregations: Min, Max, and Everything In Between | Python Data Science Handbook
In particular, their optional arguments have different meanings, and np.sum is aware of multiple array dimensions, as we will see in the following section. Similarly, Python has built-in min and max functions, used to find the minimum value and maximum value of any given array:
Programiz
programiz.com › python-programming › numpy › methods › max
NumPy max()
... The max() method returns the largest element of an array along an axis. import numpy as np array1 = np.array([10, 12, 14, 11, 5]) # return the largest element maxValue= np.max(array1) print(maxValue) # Output: 14 ... numpy.max(array, axis = None, out = None, keepdims = <no value>, initial=<no ...
DataCamp
datacamp.com › doc › numpy › max
NumPy max()
In this example, `np.max(array)` returns `7`, the maximum value in the entire one-dimensional array since no axis is specified.
Top answer 1 of 6
2
First slice your list and then use index on the max function:
searchArray = [10,20,30,40,50,60,100,80,90,110]
slicedArray = searchArray[3:9]
print slicedArray.index(max(slicedArray))+3
This returns the index of the sliced array, plus the added beginSlice
2 of 6
1
Try this...Assuming you want the index of the max in the whole list -
import numpy as np
searchArray = [10,20,30,40,50,60,100,80,90,110]
start_index = 3
end_index = 8
print (np.argmax(searchArray[start_index:end_index+1]) + start_index)
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .max()
Python:NumPy | Built-in Functions | .max() | Codecademy
July 2, 2025 - The .max() function in NumPy returns the maximum value of an array or the maximum values along a specified axis. This function is essential for data analysis, statistical computations, and finding peak values in datasets.