If you don't have any other indexes or sorted information for your objects, then you will have to iterate until such an object is found:

next(obj for obj in objs if obj.val == 5)

This is however faster than a complete list comprehension. Compare these two:

[i for i in xrange(100000) if i == 1000][0]

next(i for i in xrange(100000) if i == 1000)

The first one needs 5.75ms, the second one 58.3µs (100 times faster because the loop 100 times shorter).

Answer from eumiro on Stack Overflow
🌐
LabEx
labex.io › tutorials › python-how-to-locate-first-occurrence-in-lists-464735
Python - How to locate first occurrence in lists
## Find first even number first_even = next((num for num in numbers if num % 2 == 0), None) ## Find first element matching complex condition complex_search = next((item for item in numbers if item > 2 and item < 5), -1) flowchart LR A[Search Method] --> B{Complexity} B -->|O(n)| C[Linear Search] B -->|O(1)| D[Direct Indexing] ... At LabEx, we recommend understanding these techniques to efficiently locate first occurrences in Python lists.
Discussions

First occurrence of a number in python list - Stack Overflow
You can do it using python Recursion as well which finds first occurrence of any given number in a list. More on stackoverflow.com
🌐 stackoverflow.com
How can I find a first occurrence of a string from a list in another string in python - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... New site design and philosophy for Stack Overflow: Starting February 24, 2026... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 0 Python to search a string for the first occurrence of any item in a list... More on stackoverflow.com
🌐 stackoverflow.com
Finding first and last index of some value in a list in Python - Stack Overflow
Python lists have the index() method, which you can use to find the position of the first occurrence of an item in a list. More on stackoverflow.com
🌐 stackoverflow.com
python - Finding the index of the first occurrence of any item in a list - Stack Overflow
For the list shown in the example: my_list = ['hi', 'babe', 'hi', 'babe', 'key', 'key'] output = [0, 1, 4] or output =[1, 1, 0, 0, 1, 0] More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com › python-first-match
How to Get the First Match From a Python List or Iterable – Real Python
May 28, 2023 - Python generator iterators are memory-efficient iterables that can be used to find the first element in a list or any iterable. They’re a core feature of Python, being used extensively under the hood.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-first-occurrence-of-true-number
Python - First Occurrence of True number - GeeksforGeeks
July 11, 2025 - When used with True it finds position of first True value in the list raising a ValueError if True is not present. ... a = [False, False, True, False, True] # Find the first occurrence of True; if not found, return -1 f = a.index(True) if True in a else -1 print(f)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-first-occurrence-of-one-list-in-another
Python - First Occurrence of One List in Another - GeeksforGeeks
July 23, 2025 - We can iterate through the first list and check for the occurrence of the second list using slicing. ... # Initializing lists a = [1, 2, 3, 4, 5, 6] b = [3, 4, 5] # Finding first occurrence index = -1 for i in range(len(a) - len(b) + 1): if ...
🌐
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 - By the end of this tutorial, you’ll have learned: ... 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.
Find elsewhere
🌐
Quora
quora.com › How-do-you-get-the-first-value-in-a-list-in-Python
How to get the first value in a list in Python - Quora
For example you have created a list named as L1 and you want to know the first element So you can just do this in python terminal >>>L1[0] ... Former Systems Programmer, Chief Designer (1982–2021) · Author has 3.6K answers and 1.4M answer views · 3y · Originally Answered: What is the easiest way to find the first occurrence ...
🌐
CodeSpeedy
codespeedy.com › home › recursively find first occurrence of a number in a list using python
Recursively find first occurrence of a number in a list using Python
June 9, 2019 - Recursion is a process where a function calls itself but with a base condition. A base condition is required in order to stop a function to call itself again. In this tutorial, we will learn how to find the first occurrence of a number in a list recursively in Python.
🌐
TutorialsPoint
tutorialspoint.com › python-first-occurrence-of-one-list-in-another
First Occurrence of One List in Another in Python
The first list is : [23, 64, 34, 77, 89, 9, 21] The second list is : [64, 10, 18, 11, 0, 21] The result is : 64
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.

🌐
Python-Fiddle
python-fiddle.com › challenges › find-first-occurrence-index
Find First Occurrence Index - Python Challenge
#### Example Usage ```python [main.nopy] find_first_occurrence([2, 5, 5, 5, 6, 6, 8, 9, 9, 9], 5) # Output: 1 find_first_occurrence([2, 3, 5, 5, 6, 6, 8, 9, 9, 9], 5) # Output: 2 find_first_occurrence([2, 4, 1, 5, 6, 6, 8, 9, 9, 9], 6) # Output: 4 ``` #### Constraints - The input list `A` is sorted in non-decreasing order.
🌐
LabEx
labex.io › tutorials › python-how-to-find-first-matching-element-in-python-421206
How to find first matching element in Python | LabEx
Python offers multiple methods to find the first matching element in a collection, each with unique characteristics and use cases. ## Finding element index fruits = ['apple', 'banana', 'cherry', 'date'] try: banana_index = fruits.index('banana') print(f"Banana found at index: {banana_index}") except ValueError: print("Element not found") ## Finding first matching element numbers = [10, 15, 20, 25, 30] first_over_20 = next((num for num in numbers if num > 20), None) print(f"First number over 20: {first_over_20}")