Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Find the Maximum Value in an Array in Python (and Its Index)

Use max() with enumerate() to get a Python list’s maximum and its first index in one pass. For NumPy arrays, use argmax() and account for dimensions, ties, empty inputs, and NaNs.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a regular Python list, use max() with enumerate() to get the maximum value and its zero-based index in one pass. For a NumPy array, use np.argmax() for the index and retrieve the value at that position.

Find the maximum value and index in a Python list

enumerate() pairs each value with its index, and max() can compare those pairs by the value. Because enumerate() starts at zero by default, the returned index is zero-based.

values = [4, 12, 7, 12, 3]

index, value = max(enumerate(values), key=lambda pair: pair[1])
print(value)  # 12
print(index)  # 1

The key function tells max() to compare each pair using its second item, the value. If the maximum occurs more than once, Python returns the first maximal item encountered, so this example gives index 1, not 3. See the Python 3.13 built-in functions reference.

Choose another approach when it fits better

Get the value first, then find its first index

For a short, reusable list, two straightforward calls can be easy to read:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
value = max(values)
index = values.index(value)

list.index() returns the first matching position. This method scans for the maximum and then searches again for its first occurrence; use the enumerate() approach when you want both results in one pass.

Use an explicit loop for custom handling

A loop is useful when you want to make validation or tie behavior explicit. Check that the list is nonempty, initialize the best value and index from its first element, then update them only when a strictly larger value appears. Using a strict comparison preserves the first occurrence on ties.

if not values:
    raise ValueError("values must not be empty")

best_index = 0
best_value = values[0]

for index, value in enumerate(values[1:], start=1):
    if value > best_value:
        best_index = index
        best_value = value

Starting the comparison from the first element also handles lists whose values are all negative; initializing the best value to 0 would give an incorrect result for such a list.

Find the maximum in a NumPy array

One-dimensional array

For a one-dimensional NumPy array, np.argmax() returns the index of the maximum. Use that index to retrieve the value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np

array = np.array([4, 12, 7, 12, 3])
index = np.argmax(array)
value = array[index]

NumPy documents that argmax() returns the index into the flattened array by default. When the maximum is tied, it returns the first occurrence. See the NumPy 2.0 argmax reference.

Multidimensional array

For a multidimensional array, decide whether you need an index along a particular axis or the coordinates of one overall maximum.

  • Along an axis: pass axis= to np.argmax() to get indices along that axis.
  • Coordinates of the overall maximum: convert the flattened index into coordinates with np.unravel_index().
flat_index = np.argmax(array)
coordinates = np.unravel_index(flat_index, array.shape)
value = array[coordinates]

The resulting coordinate tuple can index the original array. See the NumPy 2.0 unravel_index reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle empty input and NaNs deliberately

Empty Python list

Calling max() on an empty iterable without a default raises ValueError. For the index-and-value recipe, checking first is clearer than relying on a default, because a default supplies one value rather than an index-value pair.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if values:
    index, value = max(enumerate(values), key=lambda pair: pair[1])
else:
    index = value = None  # Choose a sentinel that suits your application

None is only one possible application convention; callers may instead raise an error or handle the empty case another way. Python documents the empty-iterable behavior and the default argument in its built-in functions reference.

NaN values in NumPy

NumPy’s max() propagates NaNs, while nanmax() ignores them. Do not assume that argmax() ignores NaNs or that the value and index functions have matching NaN behavior. If you need a NaN-aware index, consult the nanargmax() documentation for your installed NumPy version and decide how your code should handle all-NaN or empty slices. The NumPy 2.0 max reference documents its NaN behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.