Sequences have a method index(value) which returns index of first occurrence - in your case this would be verts.index(value).

You can run it on verts[::-1] to find out the last index. Here, this would be len(verts) - 1 - verts[::-1].index(value)

Answer from SilentGhost on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-find-the-index-of-the-first-occurrence-of-the-specified-item-in-the-array
Python Array index() Method
May 29, 2023 - The Python array index() method returns smallest index value of the first occurrence of element in an array. Following is the syntax of the Python array index method โˆ’ This method accepts following parameters.
๐ŸŒ
Data Science Dojo
discuss.datasciencedojo.com โ€บ python
How can I find the index of the first occurrence of a value in a NumPy array? - Python - Data Science Dojo Discussions
May 8, 2023 - Here is an example of my array: import numpy as np arr = np.array([4, 2, 3, 1, 4]) I want to find the index of the first occurrence of the value 4, which should be 0. I tried using the numpy.where() function, but it returns a tuple of arrays ...
Discussions

python - Numpy: find first index of value fast - Stack Overflow
How can I find the index of the first occurrence of a number in a Numpy array? Speed is important to me. I am not interested in the following answers because they scan the whole array and don't sto... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 22, 2015
python - Is there a NumPy function to return the first index of something in an array? - Stack Overflow
I know there is a method for a Python list to return the first index of something: >>> xs = [1, 2, 3] >>> xs.index(2) 1 Is there something like that for NumPy arrays? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to find index of first positive number in array of positive and negative numbers?
You will have to search on your own. Even if there was an existing function that did that, it would have to walk the array item by item. More on reddit.com
๐ŸŒ r/learnprogramming
35
15
September 19, 2021
Finding last index of some value in a list in Python
v[-1] is the last element of a list or tuple. v[-2] is the second last. The syntax where you're doing v[a:b:c], is known as slice notation. a is the start position, b is the end position, and c is the increment. a and b default to the start and end. c defaults to 1, so... v[::] refers to all elements from start to end. Useful for copying whole content somewhere, as distinct from assigning the list to a new variable. v[::2] refers to every second element v[::-1] is all the element in reverse. v[::-2] is every second element in reverse. v[5:] is all elements from 5 to the end. v[:5] is all elements from the start to < 5, so v[:5] and v[5:] do not overlap. More on reddit.com
๐ŸŒ r/learnpython
6
5
August 5, 2024
Top answer
1 of 10
165

Sequences have a method index(value) which returns index of first occurrence - in your case this would be verts.index(value).

You can run it on verts[::-1] to find out the last index. Here, this would be len(verts) - 1 - verts[::-1].index(value)

2 of 10
53

Perhaps the two most efficient ways to find the last index:

def rindex(lst, value):
    lst.reverse()
    i = lst.index(value)
    lst.reverse()
    return len(lst) - i - 1
import operator

def rindex(lst, value):
    return len(lst) - operator.indexOf(reversed(lst), value) - 1

Both take only O(1) extra space and the two in-place reversals of the first solution are much faster than creating a reverse copy. Let's compare it with the other solutions posted previously:

def rindex(lst, value):
    return len(lst) - lst[::-1].index(value) - 1

def rindex(lst, value):
    return len(lst) - next(i for i, val in enumerate(reversed(lst)) if val == value) - 1

Benchmark results, my solutions are the red and green ones:

This is for searching a number in a list of a million numbers. The x-axis is for the location of the searched element: 0% means it's at the start of the list, 100% means it's at the end of the list. All solutions are fastest at location 100%, with the two reversed solutions taking pretty much no time for that, the double-reverse solution taking a little time, and the reverse-copy taking a lot of time.

A closer look at the right end:

At location 100%, the reverse-copy solution and the double-reverse solution spend all their time on the reversals (index() is instant), so we see that the two in-place reversals are about seven times as fast as creating the reverse copy.

The above was with lst = list(range(1_000_000, 2_000_001)), which pretty much creates the int objects sequentially in memory, which is extremely cache-friendly. Let's do it again after shuffling the list with random.shuffle(lst) (probably less realistic, but interesting):

All got a lot slower, as expected. The reverse-copy solution suffers the most, at 100% it now takes about 32 times (!) as long as the double-reverse solution. And the enumerate-solution is now second-fastest only after location 98%.

Overall I like the operator.indexOf solution best, as it's the fastest one for the last half or quarter of all locations, which are perhaps the more interesting locations if you're actually doing rindex for something. And it's only a bit slower than the double-reverse solution in earlier locations.

All benchmarks done with CPython 3.9.0 64-bit on Windows 10 Pro 1903 64-bit.

๐ŸŒ
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.
๐ŸŒ
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 - If you prefer working with Python lists, you can convert your NumPy array to a list and then use the built-in list.index() method to find the index of the first occurrence of an element.
๐ŸŒ
Real Python
realpython.com โ€บ python-first-match
How to Get the First Match From a Python List or Iterable โ€“ Real Python
March 18, 2026 - This tutorial will cover how best to approach all three scenarios. One option is to transform your whole iterable to a new list and then use .index() to find the first item matching your criterion:
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - Python's built-in index() function is a useful tool for finding the index of a specific element in a sequence. This function takes an argument representing the value to search for and returns the index of the first occurrence of that value in the sequence.
Top answer
1 of 15
45

Although it is way too late for you, but for future reference: Using numba (1) is the easiest way until numpy implements it. If you use anaconda python distribution it should already be installed. The code will be compiled so it will be fast.

@jit(nopython=True)
def find_first(item, vec):
    """return the index of the first occurence of item in vec"""
    for i in xrange(len(vec)):
        if item == vec[i]:
            return i
    return -1

and then:

>>> a = array([1,7,8,32])
>>> find_first(8,a)
2
2 of 15
31

I've made a benchmark for several methods:

  • argwhere
  • nonzero as in the question
  • .tostring() as in @Rob Reilink's answer
  • python loop
  • Fortran loop

The Python and Fortran code are available. I skipped the unpromising ones like converting to a list.

The results on log scale. X-axis is the position of the needle (it takes longer to find if it's further down the array); last value is a needle that's not in the array. Y-axis is the time to find it.

The array had 1 million elements and tests were run 100 times. Results still fluctuate a bit, but the qualitative trend is clear: Python and f2py quit at the first element so they scale differently. Python gets too slow if the needle is not in the first 1%, whereas f2py is fast (but you need to compile it).

To summarize, f2py is the fastest solution, especially if the needle appears fairly early.

It's not built in which is annoying, but it's really just 2 minutes of work. Add this to a file called search.f90:

subroutine find_first(needle, haystack, haystack_length, index)
    implicit none
    integer, intent(in) :: needle
    integer, intent(in) :: haystack_length
    integer, intent(in), dimension(haystack_length) :: haystack
!f2py intent(inplace) haystack
    integer, intent(out) :: index
    integer :: k
    index = -1
    do k = 1, haystack_length
        if (haystack(k)==needle) then
            index = k - 1
            exit
        endif
    enddo
end

If you're looking for something other than integer, just change the type. Then compile using:

f2py -c -m search search.f90

after which you can do (from Python):

import search
print(search.find_first.__doc__)
a = search.find_first(your_int_needle, your_int_array)
Find elsewhere
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ is-there-a-numpy-function-to-return-the-first-index-of-something-in-an-array
Is There a NumPy Function to Return the First Index of an Element in an Array? โ€” pythontutorials.net
If your array is sorted, np.searchsorted(arr, x) is a faster alternative. It finds the position where x would be inserted to maintain sorted orderโ€”which is the first index of x if x already exists (for non-unique sorted arrays, use the side ...
๐ŸŒ
Devgex
devgex.com โ€บ en โ€บ article โ€บ 00001821
Comprehensive Guide to Finding First Occurrence Index in NumPy Arrays - DevGex
October 28, 2025 - Unlike Python native lists, NumPy ... is crucial for mastering array operations. In Python lists, we can use the list.index() method to quickly find the first occurrence of an element:...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-first-occurrence-of-true-number
Python - First Occurrence of True number - GeeksforGeeks
July 11, 2025 - a = [False, False, True, False, ... True in a checks if True exists in the list if found a.index(True) returns the first index of True otherwise -1 is assigned....
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ data structures and algorithms in python โ€บ first occurrence in a sorted array
First Occurrence in a Sorted Array | PrepInsta
July 10, 2025 - The first occurrence of 4 is at index 3. Performs binary search on a sorted array to find the target element.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find-index-of-element-in-array-python
Find Index of Element in Array - Python - GeeksforGeeks
July 23, 2025 - If we want more control over the process or if we're working with arrays where we need to find the index of multiple occurrences of an element, a loop can be a good choice. ... import array arr = array.array('i', [10, 20, 30, 20, 50]) # Loop through the array to find the index of 20 for i in range(len(arr)): if arr[i] == 20: print(i) break ... This approach is useful when we're working with arrays that may contain duplicate elements and we want to find the first match or all occurrences.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
Explanation: a.index("blue") returns the index of the first occurrence of "blue" and later occurrences are ignored.
Published: July 17, 2026
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python find index of value in array
How to Find the Index of an Element in a List in Python | Delft Stack
February 2, 2024 - Python list has a built-in method called index(), which accepts a single parameter representing the value to search within the existing list. The function returns the index of the first occurrence that it finds starting from index 0 regardless ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_index.asp
Python List index() Method
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... The index() method returns the position at the first occurrence of the specified value....
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python list index: find first, last or all occurrences
Python List Index: Find First, Last or All Occurrences โ€ข datagy
February 28, 2022 - The Python list.index() method returns the index of the item specified in the list. The method will return only the first instance of that item.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-locate-first-occurrence-in-lists-464735
Python - How to locate first occurrence in lists
Understanding list indexing is fundamental to manipulating and searching through list data effectively. Python uses zero-based indexing, meaning the first element is at index 0.
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ how-to-get-the-index-of-an-item-in-a-list-in-python
How to Get the Index of an Item in a List in Python - StrataScratch
September 6, 2024 - Otherwise, Python raises a ValueError. However, it will break your program if it is not handled correctly. ... Consider you are reading sensor readings and want to get the first occurrence of a specific type of reading. You should try catching it here so the program will not crash and produce an error if no such reading exists. sensor_readings = [50, 55, 60, 65, 70] def find_reading_index(reading, readings): try: return readings.index(reading) except ValueError: return "Reading not found in the list" result = find_reading_index(65, sensor_readings) print(result) result_not_found = find_reading_index(75, sensor_readings) print(result_not_found)