Have you thought about using Python list's .index(value) method? It return the index in the list of where the first instance of the value passed in is found.

Answer from Aaron on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-index-of-element-in-array-in-python
Find index of element in array in python - GeeksforGeeks
July 23, 2025 - We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we are looking for.
Discussions

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 element? - Python - Data Science Dojo Discussions
Is it possible to find the index of the 100th element? I have created a random array using NumPy’s function. Now, my target is to find the index of the 100th element of my array. I’m using the code below but it is showing an error. import numpy as np # create a 3-dimensional array of shape ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
3
0
February 21, 2023
python - How can I find the index for a given item in a list? - Stack Overflow
If the list is short it's no problem making a copy of it from a Python list, if it isn't then perhaps the developer should consider storing the elements in numpy array in the first place. 2020-01-28T12:23:12.517Z+00:00 ... Save this answer. ... Show activity on this post. For a list ["foo", "bar", "baz"] and an item in the list "bar", what's the cleanest way to get its index ... More on stackoverflow.com
🌐 stackoverflow.com
Why no .get(idx[, default]) on python list??
There was a lengthy thread on this on Python-Ideas a couple of years ago: https://mail.python.org/archives/list/python-ideas@python.org/thread/LLK3EQ3QWNDB54SEBKJ4XEV4LXP5HVJS/ The clearest explanation of the most common objection was from Marc-Andre Lemburg: dict.get() was added since the lookup is expensive and you want to avoid having to do this twice in the common case where the element does exist. It was not added as a way to hide away an exception, but instead to bypass having to generate this exception in the first place. dict.setdefault() has a similar motivation. list.get() merely safes you a line of code (or perhaps a few more depending on how you format things), hiding away an exception in case the requested index does not exist. If that's all you want, you're better off writing a helper which hides the exception for you. I argue that making it explicit that you're expecting two (or more) different list lengths in your code results in more intuitive and maintainable code, rather than catching IndexErrors (regardless of whether you hide them in a method, a helper, or handle them directly). So this is more than just style, it's about clarity of intent. More on reddit.com
🌐 r/Python
94
144
June 23, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-index-of-element-in-array-python
Find Index of Element in Array - Python - GeeksforGeeks
July 23, 2025 - It is simple to use and works well when we know the element exists in the array. It's efficient for arrays that do not have duplicate elements since it returns the first occurrence of the element. ... import array arr = array.array('i', [10, 20, 30, 40, 50]) # Find the index of the element 30 idx = arr.index(30) print(idx)
🌐
Python Guides
pythonguides.com › find-the-index-of-an-element-in-an-array-in-python
How To Find The Index Of An Element In An Array In Python?
March 19, 2025 - Learn how to find the index of an element in a Python array (or list) using methods like `index()`, loops, and NumPy's `where()`. Step-by-step examples included
🌐
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?

🌐
Pythonspot
pythonspot.com › array-find
Python Find In Array — Tutorial with Examples | Pythonspot
January 1, 2026 - Array duplicates: If the array contains duplicates, the index() method will only return the first element. If you want multiple to find multiple occurrences of an element, use the lambda function below.
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index()
The index() method returns the index of the specified element in the list.
Find elsewhere
🌐
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 - Create a NumPy array and iterate over the array to compare the element in the array with the given array. If the element matches print the index.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_search.asp
NumPy Searching Arrays
There is a method called searchsorted() which performs a binary search in the array, and returns the index where the specified value would be inserted to maintain the search order. The searchsorted() method is assumed to be used on sorted arrays. Find the indexes where the value 7 should be inserted:
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-element-in-array-python
Find element in Array - Python - GeeksforGeeks
July 23, 2025 - ... import array # Create an array ... print(f"Not found") ... If we want to find the index of an item in the array, we can use the index() method....
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to find index of element? - Python - Data Science Dojo Discussions
February 21, 2023 - Is it possible to find the index of the 100th element? I have created a random array using NumPy’s function. Now, my target is to find the index of the 100th element of my array. I’m using the code below but it is showing an error. import numpy as np # create a 3-dimensional array of shape (10, 10, 10) arr = np.arange(1000).reshape((10, 10, 10)) indices = np.argpartition(arr.flatten(99)) index = np.unravel_index(indices[-1], arr.shape) print(index) TypeError Tr...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python array index
Python Array Index - Spark By {Examples}
May 31, 2024 - Python Array index is commonly used to refer to the index of an element within an array. You can use array indexing to manipulate and access array
Top answer
1 of 16
6115
>>> ["foo", "bar", "baz"].index("bar")
1

See the documentation for the built-in .index() method of the list:

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.

Caveats

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If the list is long, and if there is no guarantee that the value will be near the beginning, this can slow down the code.

This problem can only be completely avoided by using a different data structure. However, if the element is known to be within a certain part of the list, the start and end parameters can be used to narrow the search.

For example:

>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514

The second call is orders of magnitude faster, because it only has to search through 10 elements, rather than all 1 million.

Only the index of the first match is returned

A call to index searches through the list in order until it finds a match, and stops there. If there could be more than one occurrence of the value, and all indices are needed, index cannot solve the problem:

>>> [1, 1].index(1) # the `1` index is not found.
0

Instead, use a list comprehension or generator expression to do the search, with enumerate to get indices:

>>> # A list comprehension gives a list of indices directly:
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> # A generator comprehension gives us an iterable object...
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> # which can be used in a `for` loop, or manually iterated with `next`:
>>> next(g)
0
>>> next(g)
2

The list comprehension and generator expression techniques still work if there is only one match, and are more generalizable.

Raises an exception if there is no match

As noted in the documentation above, using .index will raise an exception if the searched-for value is not in the list:

>>> [1, 1].index(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 2 is not in list

If this is a concern, either explicitly check first using item in my_list, or handle the exception with try/except as appropriate.

The explicit check is simple and readable, but it must iterate the list a second time. See What is the EAFP principle in Python? for more guidance on this choice.

2 of 16
725

The majority of answers explain how to find a single index, but their methods do not return multiple indexes if the item is in the list multiple times. Use enumerate():

for i, j in enumerate(['foo', 'bar', 'baz']):
    if j == 'bar':
        print(i)

The index() function only returns the first occurrence, while enumerate() returns all occurrences.

As a list comprehension:

[i for i, j in enumerate(['foo', 'bar', 'baz']) if j == 'bar']

Here's also another small solution with itertools.count() (which is pretty much the same approach as enumerate):

from itertools import izip as zip, count # izip for maximum efficiency
[i for i, j in zip(count(), ['foo', 'bar', 'baz']) if j == 'bar']

This is more efficient for larger lists than using enumerate():

$ python -m timeit -s "from itertools import izip as zip, count" "[i for i, j in zip(count(), ['foo', 'bar', 'baz']*500) if j == 'bar']"
10000 loops, best of 3: 174 usec per loop
$ python -m timeit "[i for i, j in enumerate(['foo', 'bar', 'baz']*500) if j == 'bar']"
10000 loops, best of 3: 196 usec per loop
🌐
datagy
datagy.io › home › python posts › python lists › python: find list index of all occurrences of an element
Python: Find List Index of All Occurrences of an Element • datagy
December 30, 2022 - NumPy makes the process of finding all index positions of an element in a list very easy and fast. This can be done by using the where() function. The where() function returns the index positions of all items in an array that match a given value.
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - In this tutorial, you will learn about the Python index() function. The index() method searches an element in the list and returns its position/index. First, this tutorial will introduce you to lists, and then you will see some simple examples of how to work with the index() function.
🌐
Flexiple
flexiple.com › python › find-index-of-element-in-list-python
Find index of element in list Python | Flexiple Tutorials | Python - Flexiple
March 10, 2022 - Discover how to find the index of an element in a list using Python. Our concise guide simplifies the process for efficient programming.
🌐
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.
🌐
Python Examples
pythonexamples.org › python-find-index-of-item-in-list
How to find index of an item in a list?
To find the index of a specific element in a given list in Python, you can call the list.index() method on the list object and pass the specific element as argument.