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 DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Find an Element’s Index in a Python Array

Use list.index() for the first matching list position, or NumPy where/nonzero to find matching array positions and multidimensional coordinates.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a regular Python list, use items.index(value) to get the zero-based position of its first match. For a NumPy array, compare the array with the value and use np.where() to find matching positions. The right method depends on whether “array” means a list or a NumPy ndarray, and whether you need one match or all of them.

Find an element’s index in a Python list

Call the list’s index() method:

items = ["red", "blue", "green"]
position = items.index("blue")
print(position)  # 1

Python list positions start at zero, so the first item is at index 0. The method returns the first occurrence of the value. If the value is not in the list, it raises ValueError. See the Python 3.14.8 tutorial for the documented behavior.

Search within part of a list

list.index(value[, start[, stop]]) accepts optional start and stop bounds. The search is limited to that portion, but the returned index remains relative to the whole list:

items = ["red", "blue", "green", "blue"]
position = items.index("blue", 2)  # 3

Find the first match, all matches, or handle no match

Because index() returns only the first match, use a list comprehension with enumerate() when you need every matching position:

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.
items = ["red", "blue", "green", "blue"]
target = "blue"
positions = [i for i, value in enumerate(items) if value == target]
print(positions)  # [1, 3]

If nothing matches, this produces an empty list. By contrast, items.index(target) raises ValueError. Use index() when absence should be treated as an error; collect positions when zero, one, or many matches are all expected outcomes.

Find matching positions in a NumPy array

NumPy arrays do not use the list’s .index() method. Compare the array with the target; np.where() returns the positions where the comparison is true. For a one-dimensional array, select the first index array from its result:

import numpy as np

arr = np.array([10, 20, 30, 20])
positions = np.where(arr == 20)[0]
print(positions)  # [1 3]

This finds all matches. An empty result means no element matched. NumPy uses zero-based indexing; its where documentation describes the function’s behavior.

Represent matches in a multidimensional NumPy array

For a two-dimensional array, each match has a row and a column coordinate. Choose the result format based on what you will do with those coordinates:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
arr = np.array([[4, 7], [7, 9]])
coordinates = np.argwhere(arr == 7)  # [[0, 1], [1, 0]]
index_arrays = np.nonzero(arr == 7)  # (array([0, 1]), array([1, 0]))
  • np.argwhere(condition) presents matches as coordinate rows. For this example, the matches are at row 0, column 1 and row 1, column 0. Its output has shape (number_of_matches, number_of_dimensions). NumPy cautions that this output is not suitable for indexing an array; see the argwhere documentation.
  • np.nonzero(condition) returns one integer index array per dimension. Use it when you want index arrays for indexing the original array. NumPy documents this per-dimension indexing in its indexing guide.

Keep the per-axis coordinates when row and column matter. A flattened index is useful only when the application specifically needs a single position in a flattened one-dimensional sequence.

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

Which “Python array” do you have?

The phrase can mean a regular list, a NumPy ndarray, or Python’s separate standard-library array type. These are distinct data structures, so identify the type before choosing a lookup method. The built-in list method is documented in the Python tutorial; the standard-library type is described in the array module documentation; NumPy’s array indexing behavior is covered in its indexing guide.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.