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...except case 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!

Answer from Martijn Pieters on Stack Overflow
🌐
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.
Discussions

how to get the maximum value from a specific portion of a array in python - Stack Overflow
I have a specific scenario that I need to scan a specific portion of an array for a maximum value of that portion and return the position of that value with regards to the entire array. for example More on stackoverflow.com
🌐 stackoverflow.com
Does nim have an equivalent to python's max function?
Does sequtils.maxIndex do what you want? It works on anything that can be < > compared, I think. nim> maxIndex([1,2,3]) 2 == type int It returns the index of the largest element, which you could then use to get that element? https://nim-lang.org/docs/sequtils.html#maxIndex%2CopenArray%5BT%5D More on reddit.com
🌐 r/nim
2
7
July 27, 2021
python max() in java
u/dbc2201 already mentioned streams, but there is one more thing which comes relatively close. So if you store your data in a list instead of an array, then you can use the Collections.max function: List xs = new ArrayList<>(List.of(2, 4, 3)); // example values System.out.println(Collections.max(xs)); You can also add a custom comparator if needed (thanks to overloading), so that you can compare objects of any datatype. More on reddit.com
🌐 r/learnjava
13
3
August 10, 2021
min() vs 'sort() and then list[0]'

As others have mentioned, min will be quicker. min just has to walk through the list once, whereas sort must do more compares, making it more complex.

However, one case you might want to use sort is when you need more than just the minimum. Eg. you want the 5 smallest items, not just the smallest. Here "sorted(mylist)[:5]" will do the job, but another option to keep in mind is the heapq module, as "heapq.nsmallest(5, mylist)" will often perform better than sort, at least for a small number of items.

More on reddit.com
🌐 r/Python
36
29
October 1, 2010
🌐
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'
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-find-largest-element-in-an-array
Python Program to Find Largest Element in an Array - GeeksforGeeks
October 22, 2025 - Given an array, we need to find the largest element in it. ... Python has an inbuilt method max() which returns the maximum value among the arguments.
🌐
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.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › max
Python Numpy max() - Find Maximum Value | Vultr Docs
November 18, 2024 - Create a one-dimensional NumPy array. Use max() to find the maximum value.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to calculate maximum of array in numpy
How to Calculate Maximum of Array in NumPy - Spark By {Examples}
September 24, 2024 - Python NumPy maximum() or max() function is used to get the maximum value (greatest value) of a given array, or compare the two arrays
🌐
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.
🌐
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 ...
🌐
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:
🌐
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.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › maximum
Python Numpy maximum() - Find Maximum Value | Vultr Docs
November 18, 2024 - Initialize a NumPy array with numerical values. Apply numpy.maximum() to find the maximum value in the array.
🌐
Quora
quora.com › Is-there-a-built-in-function-in-Python-for-finding-the-maximum-value-in-an-array-If-so-how-does-it-work
Is there a built-in function in Python for finding the maximum value in an array? If so, how does it work? - Quora
The syntax is: max (array) [code]l = eval (input ('Enter an array: ')) print (max (l)) [/code]max () function iterates through the given array, and assigns the first-ever element as the ‘maximum value’...
🌐
HowToDoInJava
howtodoinjava.com › home › python examples › python max() and min(): find max and min in array
Python max() and min(): Find max and min in Array
September 21, 2023 - In this beginner-friendly guide, we’ll explore how to use max() and min() effectively. numbers = [3, 7, 1, 9, 4, 2] max_number = max(numbers) # 9 min_number = min(numbers) # 1 · The max() function finds the maximum value in an iterable.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.amax.html
numpy.amax — NumPy v2.1 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 › stable › reference › generated › numpy.max.html
numpy.max — NumPy v2.4 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.