Yes, given an array, array, and a value, item to search for, you can use np.where as:

itemindex = numpy.where(array == item)

The result is a tuple with first all the row indices, then all the column indices.

For example, if an array is two dimensions and it contained your item at two locations then

array[itemindex[0][0]][itemindex[1][0]]

would be equal to your item and so would be:

array[itemindex[0][1]][itemindex[1][1]]
Answer from Alex on Stack Overflow
🌐
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 - where() method is used to specify the index of a particular element specified in the condition. ... 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 ...
Discussions

python - Index of element in NumPy array - Stack Overflow
The second method returns an array ... an empty array if var is not found. In short, they are not equivalent, and have separate use cases. 2025-04-03T15:23:40.237Z+00:00 ... Save this answer. ... Show activity on this post. This problem can be solved efficiently using the numpy_indexed library (disclaimer: I am its author); which was created to address problems of this type. npi.indices can be viewed as an n-dimensional generalisation of list.index... More on stackoverflow.com
🌐 stackoverflow.com
efficiently finding the index of a value in a numpy array
You can use np.where(OPs_array==value) to get the indices. But if that is the main use of your array then consider using a dictionary. More on reddit.com
🌐 r/learnpython
1
3
September 9, 2022
How to find index of value in NumPy array? - Data Exploration & Visualization - Data Science Dojo Discussions
Suppose I have an array that contains zero and non-zero values. Now I want to find the index of non-zero values of my array. I tried many methods previously, but they are not giving me the desired result. Can someone help me with this? More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
3
0
February 17, 2023
python - get the index of element in NumPy array - Stack Overflow
Sort by the (original) index, to bring back the original order (without the dropped elements, for now we have original values with their corresponding indices). Return the index of the above result, as a Numpy array. More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
Think of 2-D arrays like a table with rows and columns, where the dimension represents the row and the index represents the column. Access the element on the first row, second column: import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('2nd element on 1st row: ', arr[0, 1]) Try it Yourself »
🌐
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 »
🌐
thisPointer
thispointer.com › home › python › find the index of value in numpy array using numpy.where()
Find the index of value in Numpy Array using numpy.where() - thisPointer
April 1, 2023 - In the above numpy array, elements with value 15 occurs at different places let’s find all it’s indices i.e. # Get the index of elements with value 15 result = np.where(arr == 15) print('Tuple of arrays returned : ', result) print("Elements with value 15 exists at following indices", result[0], sep='\n')
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.argwhere.html
numpy.argwhere — NumPy v2.2 Manual
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) where N is the number of non-zero items.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › numpy › index of element in array
How to Find the First Index of Element in NumPy Array | Delft Stack
March 11, 2025 - Therefore, it’s wise to ensure that the element exists in the array before attempting to find its index. For those who prefer a more manual approach or need to implement custom logic, using a loop to iterate through the elements of the array can be a good solution. This method provides flexibility and can be tailored to specific requirements. ... import numpy as np array = np.array([10, 20, 30, 40, 50, 20]) element_to_find = 20 index = -1 for i in range(len(array)): if array[i] == element_to_find: index = i break
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-index-of-element-in-array-in-python
Find index of element in array in python - GeeksforGeeks
July 23, 2025 - import numpy as np arr = np.array([10, 20, 30, 40, 30, 50]) element = 30 # Find indices indices = np.where(arr == element)[0] print("Indices:", indices)
🌐
Reddit
reddit.com › r/learnpython › efficiently finding the index of a value in a numpy array
r/learnpython on Reddit: efficiently finding the index of a value in a numpy array
September 9, 2022 -

I have a numpy array that has unique values and is static, and I routinely want to find some index of a value. Is it a good idea to repeatedly use where for this? Is numpy sorting the values and storing a mapping of them to the indices behind the scene, or otherwise doing something smart to quickly find the index? If not, what would be a good way to implement finding the index of a value in a numpy array?

🌐
Data Science Dojo
discuss.datasciencedojo.com › data exploration & visualization
How to find index of value in NumPy array? - Data Exploration & Visualization - Data Science Dojo Discussions
February 17, 2023 - Suppose I have an array that contains zero and non-zero values. Now I want to find the index of non-zero values of my array. I tried many methods previously, but they are not giving me the desired result. Can someone hel…
🌐
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 - This tutorial explains how to find the index location of specific values in a NumPy array, including examples.
Top answer
1 of 2
1

You can do it quite easily, using Pandas.

First convert your array to a pandasonic Series:

s = pd.Series(a)

Then:

  • Group it by its value.
  • Apply to each group a function, which:
    • for groups of size 4 or smaller returns just this group,
    • for groups with more members, returns a random sample of 4 elements from them.
  • Drop the 0-th level of the resulting index (added during grouping).
  • Sort by the (original) index, to bring back the original order (without the dropped elements, for now we have original values with their corresponding indices).
  • Return the index of the above result, as a Numpy array.

The code to do it is:

s.groupby(s).apply(lambda grp: grp if grp.size <= 4 else grp.sample(4))\
    .reset_index(level=0, drop=True).sort_index().index.values

For a sample array containg:

array([2, 2, 1, 0, 1, 0, 2, 2, 2, 3, 0, 2, 1, 0, 0, 3, 3, 0, 2, 4])

the result is:

array([ 0,  2,  4,  5,  7,  9, 10, 11, 12, 14, 15, 16, 17, 18, 19])

To show that this result is correct, I repeated the source array, with "x" marks below the elements at the returned indices.

array([2, 2, 1, 0, 1, 0, 2, 2, 2, 3, 0, 2, 1, 0, 0, 3, 3, 0, 2, 4])
       x     x     x  x     x     x  x  x  x     x  x  x  x  x  x
2 of 2
0

Yes, you can do this using NumPy by:

a = np.random.randint(0,10,20)
print(a)

num = 4
if str(np.where(a<num)[0].shape) != '(0,)':             # Condition 1
    ans = np.where(a<num)[0]
    print(ans)
if str(np.where(a>=num)[0].shape) != '(0,)':            # Condition 2
    ans = np.random.choice(a[np.where(a>=num)[0]], 4)
    print(ans)

'''Output:
[9 9 8 1 0 7 7 4 6 2 8 2 1 2 9 5 5 1 4 1]
[ 3  4  9 11 12 13 17 19]
[4 9 8 7]
'''

I have done only for the cases you have mentioned. There can be many other cases such as if both conditions are true, or if there are less than 4 numbers in second case.

🌐
CodeSpeedy
codespeedy.com › home › find the index of value in numpy array
Find the index of value in Numpy Array - CodeSpeedy
October 4, 2022 - Learn how to find the index of value in Numpy array using the numpy.where() and argsort+searchsorted() function on 1 and 2 dimensional array.
🌐
Sololearn
sololearn.com › en › Discuss › 2579971 › how-i-get-the-index-of-element-in-numpy-array
how i get the index of element in numpy array?
November 7, 2020 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
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 approach combines numpy.where() with standard array indexing to efficiently extract the initial element from the resulting index array, thus isolating the first match.
🌐
Data Science Parichay
datascienceparichay.com › article › find-index-of-element-in-numpy-array
Find Index of Element in Numpy Array
Two thumbs up - I recently switched to WPX Hosting and recommend their speed, service and security - they do know what they are talking about when it comes to WordPress hosting.
🌐
sqlpey
sqlpey.com › python › finding-element-indices-in-numpy-arrays
Finding Element Indices in NumPy Arrays: Techniques and Performance
November 4, 2025 - The result will be a tuple of three arrays, representing the indices along the depth, row, and column dimensions, respectively. ANS: No, the standard Python list .index(x) method is not natively available for NumPy arrays. You must convert the NumPy array to a list first using .tolist() before calling .index(). ANS: Calculate the absolute difference between every element and the target float using np.abs(array - target).
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.argwhere.html
numpy.argwhere — NumPy v2.5 Manual
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) where N is the number of non-zero items.