DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Find the Index of the Maximum Value in a Python List

Use max() with list.index() to find the first maximum’s zero-based index in Python, or enumerate the list to return every tied position.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a non-empty list, use values.index(max(values)) to get the zero-based index of the first occurrence of the largest value. If you need every index tied for the maximum, compare each value with max(values) while enumerating the list.

Find the first index of the maximum value

Call max() to get the largest item, then call the list’s index() method to find its first position:

As an Amazon Associate I earn from qualifying purchases.

values = [4, 9, 2, 9, 6]
max_index = values.index(max(values))
print(max_index)  # 1

The largest value is 9, which appears at indices 1 and 3. Python list indices start at zero, and list.index() returns the first occurrence of the requested value. See the Python tutorial’s list-method documentation.

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

Return every index tied for the maximum

index() returns just one position. To collect every position where the maximum occurs, find the maximum once and filter the indexed values:

values = [4, 9, 2, 9, 6]
maximum = max(values)
max_indices = [i for i, value in enumerate(values) if value == maximum]
print(max_indices)  # [1, 3]

enumerate() pairs each value with its index, and the comparison keeps each position whose value equals the maximum.

Use one pass when working with an iterable

If you want both the winning index and its value, or your input is a general iterable rather than a list, take the maximum of the indexed pairs:

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

The key function tells max() to compare each pair by its value. This scans the iterable once. For a list and a beginner-friendly expression, values.index(max(values)) is often easier to follow.

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

Handle an empty list

An empty list has no maximum: max([]) raises ValueError. Check for an item before calling max(), and decide how your program should represent the empty case:

if values:
    max_index = values.index(max(values))
else:
    max_index = None

You can also pass a default to max() for an empty iterable, but that default is not a real list index. Do not pass it to index() as though it identified an actual maximum.

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

Choose an approach based on the result you need

  • First maximum index: use values.index(max(values)).
  • All maximum indices: compute the maximum, then enumerate and filter the list.
  • Index and value from an iterable: use max(enumerate(values), key=lambda pair: pair[1]), with a separate empty-input strategy if needed.

For built-in lists, the two-step approach performs two linear scans—one for max() and one for index()—so its total time complexity is still O(n). A single-pass maximum can be useful when the iterable cannot be restarted or when you need the value as well as its position. CPython’s complexity reference lists max(l) as O(n) and sorting as O(n log n); those figures describe CPython and built-in types, so other implementations or custom classes may differ. Sorting just to locate the maximum is usually unnecessary, and list.sort() changes the list in place.

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.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

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
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

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.