TL;DR

Use:

try:
    i = numpy.where(arr == "string1")[0][0]
except IndexError:
    # handle the case where "string1" was not found in arr

or

indices = list(numpy.where(arr == "string1")[0])

Details

Finding elements in NumPy arrays is not intuitive the first time you try to do it.

Let's decompose the operation:

>>> arr = numpy.array(["string1","string2","string3"])
>>> arr == "string1"
array([ True, False, False])

Notice how just doing arr == "string1" is already doing the search: it's returning an array of booleans of the same shape as arr telling us where the condition is true.

Then, you're using numpy.where which, when used with only one parameter (the condition), returns where its input is non-zero. With booleans, that means non false.

>>> numpy.where(numpy.array([ True, False, False]))
(array([0], dtype=int64),)
>>> numpy.where(arr == "string1")
(array([0], dtype=int64),)

It's not quite clear to my where this gives you a tuple of arrays for a 1-D input, but when you use this syntax with a 2-d input, it makes more sense.

In any case, what you're getting here is a tuple containing a list of indices where the condition matches. Notice it has to be a list, because you might have multiple matches.

For your code, you want numpy.where(arr == "string1")[0][0], because you know "string1" occurs in the list, but the inner list may also contain zero or more than one values, depending on how many times the string is found.

>>> arr2 = numpy.array(["string1","string2","string3","string1", "string3"])
>>> numpy.where(arr2 == "foo")
(array([], dtype=int64),)
>>> numpy.where(arr2 == "string3")
(array([2, 4], dtype=int64),)

So when you want to use these indices, you should simply treat numpy.where(arr == "string1")[0] as a list (it's really a 1-D array, though) and continue from there.

Now, just using numpy.where(arr == "some string")[0][0] is risky, because it will throw an IndexError exception if the string is not found in arr. If you really want to do that, do it in a try/except block.

If you need the list of indices as a Python list, you can do this:

indices = list(numpy.where(arr == "string1")[0])
Answer from joanis on Stack Overflow
Top answer
1 of 2
4

TL;DR

Use:

try:
    i = numpy.where(arr == "string1")[0][0]
except IndexError:
    # handle the case where "string1" was not found in arr

or

indices = list(numpy.where(arr == "string1")[0])

Details

Finding elements in NumPy arrays is not intuitive the first time you try to do it.

Let's decompose the operation:

>>> arr = numpy.array(["string1","string2","string3"])
>>> arr == "string1"
array([ True, False, False])

Notice how just doing arr == "string1" is already doing the search: it's returning an array of booleans of the same shape as arr telling us where the condition is true.

Then, you're using numpy.where which, when used with only one parameter (the condition), returns where its input is non-zero. With booleans, that means non false.

>>> numpy.where(numpy.array([ True, False, False]))
(array([0], dtype=int64),)
>>> numpy.where(arr == "string1")
(array([0], dtype=int64),)

It's not quite clear to my where this gives you a tuple of arrays for a 1-D input, but when you use this syntax with a 2-d input, it makes more sense.

In any case, what you're getting here is a tuple containing a list of indices where the condition matches. Notice it has to be a list, because you might have multiple matches.

For your code, you want numpy.where(arr == "string1")[0][0], because you know "string1" occurs in the list, but the inner list may also contain zero or more than one values, depending on how many times the string is found.

>>> arr2 = numpy.array(["string1","string2","string3","string1", "string3"])
>>> numpy.where(arr2 == "foo")
(array([], dtype=int64),)
>>> numpy.where(arr2 == "string3")
(array([2, 4], dtype=int64),)

So when you want to use these indices, you should simply treat numpy.where(arr == "string1")[0] as a list (it's really a 1-D array, though) and continue from there.

Now, just using numpy.where(arr == "some string")[0][0] is risky, because it will throw an IndexError exception if the string is not found in arr. If you really want to do that, do it in a try/except block.

If you need the list of indices as a Python list, you can do this:

indices = list(numpy.where(arr == "string1")[0])
2 of 2
1

Method 1 : This will give you all the indices in a list : [0,3] using np.where(arr == "string1")[0] and then convert to tuple

import numpy as np

arr = np.array(["string1", "string2", "string3", "string1"])
indices_tuple = tuple(np.where(arr == "string1")[0])

print(indices_tuple)
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-find-the-index-of-value-in-numpy-array
How to find the Index of value in Numpy Array ? - GeeksforGeeks
July 23, 2025 - Here, we find all the indexes of 3 and the index of the first occurrence of 3, we get an array as output and it shows all the indexes where 3 is present. ... # import numpy package import numpy as np # create an numpy array a = np.array([1, 2, 3, 4, 8, 6, 7, 3, 9, 10]) # display index value of 3 print("All index value of 3 is: ", np.where(a == 3)[0]) print("First index value of 3 is: ",np.where(a==3)[0][0])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
NumPy
numpy.org › doc › 1.15 › reference › generated › numpy.where.html
numpy.where — NumPy v1.15 Manual
NumPy Reference · Routines · Indexing routines · index · next · previous · numpy.nonzero · numpy.indices · numpy.where(condition[, x, y])¶ · Return elements, either from x or y, depending on condition. If only condition is given, return condition.nonzero().
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_search.asp
NumPy Searching Arrays
You can search an array for a certain value, and return the indexes that get a match. To search an array, use the where() method. ... import numpy as np arr = np.array([1, 2, 3, 4, 5, 4, 4]) x = np.where(arr == 4) print(x) Try it Yourself »
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.argwhere.html
numpy.argwhere — NumPy v2.2 Manual
numpy.argwhere(a)[source]# Find the indices of array elements that are non-zero, grouped by element. Parameters: aarray_like · Input data. Returns: index_array(N, a.ndim) ndarray · Indices of elements that are non-zero. Indices are grouped by element. This array will have shape (N, a.ndim) ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.where.html
numpy.where — NumPy v1.13 Manual
NumPy Reference · Routines · Indexing routines · index · next · previous · numpy.where(condition[, x, y])¶ · Return elements, either from x or y, depending on condition. If only condition is given, return condition.nonzero(). See also · nonzero, choose ·
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
🌐
NumPy
numpy.org › doc › 1.13 › reference › generated › numpy.where.html
numpy.where — NumPy v1.13 Manual
NumPy Reference · Routines · Indexing routines · index · next · previous · numpy.nonzero · numpy.indices · numpy.where(condition[, x, y])¶ · Return elements, either from x or y, depending on condition. If only condition is given, return condition.nonzero().
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-where-in-python
numpy.where() in Python - GeeksforGeeks
September 30, 2025 - With x and y: returns a new array choosing from x where True, y where False (supports broadcasting and dtype rules). In this example, numpy.where() checks where the condition arr % 2 == 0 is true and returns the indices.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › where()
Python Numpy where() - Conditional Element Search | Vultr Docs
December 30, 2024 - Use numpy.where() to find indices of elements that satisfy a specific condition.
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily find the index of a value in a numpy array
How To Easily Find The Index Of A Value In A NumPy Array
December 4, 2025 - This method is the definitive standard for comprehensive index retrieval, utilizing the core functionality of numpy.where(). When used with a conditional statement (e.g., x == 8), the function returns the indices where the specified condition is met across all dimensions of the NumPy structure.
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-find-indices-of-a-given-value-in-a-numpy-array-or-matrix-in-python-
How to find indices of a given value in a numpy array (or matrix) in python ?
April 13, 2021 - To find the indice of the value 7 for example, a solution is to use numpy where: np.where(a==7) returns here: (array([0]),) meaning that 7 is at the index 0. Note that np.where() returns a tuple object: type(np.where(a==7)) gives · tuple · Another example ·
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python numpy howtos" › "how to find the first index of element in numpy array"
How to Find the First Index of Element in NumPy Array | Delft Stack
March 11, 2025 - One of the simplest and most effective methods to find the first index of an element in a NumPy array is by utilizing the np.where() function. This function returns the indices of elements that satisfy a given condition.
🌐
Statology
statology.org › home › numpy: how to get indices where value is true
NumPy: How to Get Indices Where Value is True
August 22, 2022 - import numpy as np #create NumPy matrix my_matrix = np.array([[2, 5, 9, 12], [6, 7, 8, 8], [2, 5, 7, 8], [4, 1, 15, 11]]) #get index of rows where any value is greater than 10 np.asarray(np.any(my_matrix>10, axis=1)).nonzero() (array([0, 3], dtype=int32),)
🌐
Statology
statology.org › home › how to find index of value in numpy array (with examples)
How to Find Index of Value in NumPy Array (With Examples)
September 17, 2021 - You can use the following methods to find the index position of specific values in a NumPy array: