October 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 NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Get the Index of an Element in a Python List (Including All Matches)

Use list.index() for the first matching position, or enumerate() to collect every matching index. Learn what happens when the value is missing and how search bounds work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use items.index(target) to get the zero-based index of the first matching element in a Python list. If you need every matching position, use enumerate() in a list comprehension instead.

Get the first matching index with list.index()

Call .index(value) on the list. Python returns the position of the first element equal to that value, counting from zero:

As an Amazon Associate I earn from qualifying purchases.

items = ["red", "blue", "red"]
target = "red"

first_index = items.index(target)
print(first_index)  # 0

In the Python 3.14.8 tutorial, the method is described as returning the “zero-based index of the first occurrence of value in the list.” Python documentation: Data Structures.

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

Handle a value that is not in the list

If no element equals the requested value, list.index() raises ValueError. Catch that exception when a missing value is an expected outcome:

try:
    first_index = items.index(target)
except ValueError:
    first_index = None

You can also test membership before looking up the index:

if target in items:
    first_index = items.index(target)
else:
    first_index = None

This second approach searches the list twice when the value is present: once for in and again for index(). Catching ValueError avoids that repeated search.

Find every matching index

list.index() stops at the first match. To collect all matching positions, pair each value with its position using enumerate(), then keep the positions whose values equal the target:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items = ["red", "blue", "red"]
target = "red"

all_indexes = [i for i, value in enumerate(items) if value == target]
print(all_indexes)  # [0, 2]

If nothing matches, the comprehension returns an empty list, []. This is often clearer than repeatedly calling index() when you do not know how many matches to expect. Python’s tutorial documents enumerate() for retrieving a sequence’s position and corresponding value: Python documentation: Data Structures.

Limit the search with start and stop

items.index(target, start, stop) searches from start up to, but not including, stop. These bounds restrict which list elements are searched; if a match is found, its result is still an index in the original list.

items = ["red", "blue", "red"]

later_index = items.index("red", 1, 3)
print(later_index)  # 2

Here the search begins at position 1, so the match at position 0 is skipped. The returned 2 is not an offset relative to the search start. The official tutorial also demonstrates using a starting position to search for a later occurrence: Python documentation: Data Structures.

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

Choose the method that matches the result you need

Approach What it returns When there is no match
items.index(target) The first matching index Raises ValueError
[i for i, value in enumerate(items) if value == target] A list of all matching indexes Returns []
items.count(target) The number of matching elements, not their indexes Returns 0

Use count() only when you need the number of matches, not their positions. See the Python documentation for list methods.

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.

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.