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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-first-occurrence-of-true-number
Python - First Occurrence of True number - GeeksforGeeks
July 11, 2025 - For loop iterates through enumerate(a), checking each value; when True is found its index is assigned to f and loop exits with break.
Discussions

python - Search over a list and return the index at first occurrence then assign - 1 if value not there - Stack Overflow
Hi there im a begginer (not great at it) and currently stuck on a question for assignment question is as follows "Write a function called find(my_list, value) that takes a list, and a value as More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Find index of first occurrence in sorted list - Stack Overflow
I have a sorted list that looks like this: sortedlist = ['0','0','0','1','1,'1,'2',2','3'] I also have a count variable: count = '1' *note: sometimes count can be an integar greater that the max ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python to search a string for the first occurrence of any item in a list - Stack Overflow
I have to parse a few thousand txt documents using python, but right now I'm getting the code working for just one. I am trying to find the first time any month (January, February, March, etc) app... More on stackoverflow.com
๐ŸŒ stackoverflow.com
First occurrence of a number in python list - Stack Overflow
I have a long list which consist of two fixed possible numbers (0 and 1). e.g: l = [0,0,0,0,0,1,1,1,1,1,1,1] I don't know which number occurs first in the list but I am sure that if 0 is occurring More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ find index of first occurrence in list in python (2 examples)
Find Index of First Occurrence of Item in List in Python (Example)
May 4, 2023 - We, therefore, create the variable element_to_find = 3, which will be called in both examples. In this first example, we will use a for loop and the enumerate() function to get the index of the first occurrence of 3 in the list:
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-locate-first-occurrence-in-lists-464735
Python - How to locate first occurrence in lists
flowchart TD A[Start List Indexing] --> B{What do you want to do?} B --> |Find Element| C[Use index() method] B --> |Count Occurrences| D[Use count() method] B --> |Access Specific Position| E[Use direct indexing] When an index is out of range, Python raises an IndexError:
๐ŸŒ
Real Python
realpython.com โ€บ python-first-match
How to Get the First Match From a Python List or Iterable โ€“ Real Python
May 28, 2023 - The for loop approach is the one taken by the first package, which is a tiny package that you can download from PyPI that exposes a general-purpose function, first(). This function returns the first truthy value from an iterable by default, ...
๐ŸŒ
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 - Unfortunately, Python doesnโ€™t provide an easy method to do this. However, we can make use of incredibly versatile enumerate() function and a for-loop to do this.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-first-occurrence-of-one-list-in-another
Python - First Occurrence of One List in Another - GeeksforGeeks
July 23, 2025 - # Initializing lists a = [1, 2, 3, 4, 5, 6] b = [3, 4, 5] # Finding first occurrence using next() index = next((i for i in range(len(a) - len(b) + 1) if a[i:i + len(b)] == b), -1) print(index) ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-first-occurrence-of-one-list-in-another
Python โ€“ First Occurrence of One List in Another | GeeksforGeeks
January 31, 2025 - If the second list appears as a subsequence inside the first list, we return the starting index of its first occurrence; otherwise, we return -1. For example, if a = [1, 2, 3, 4, 5, 6] and b = [3, 4, 5], the result should be 2 because [3, 4, ...
๐ŸŒ
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 ... 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 of an element ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 56485424 โ€บ search-over-a-list-and-return-the-index-at-first-occurrence-then-assign-1-if-v โ€บ 56485578
python - Search over a list and return the index at first occurrence then assign - 1 if value not there - Stack Overflow
def find(my_list, value): index = 0 k = -1 for element in my_list: if element != value : index += 1 if element == value: k = index return k ... Your return is inside your for loop, which means it returns after one iteration of the loop.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-occurrences-of-i-before-first-j-in-list
Python program to Occurrences of i before first j in list - GeeksforGeeks
July 23, 2025 - Initialize the variable count_i to 0 to keep track of the number of occurrences of i before the first occurrence of j. Loop through the elements of the list using a for loop. For each element, check if it is equal to j. If it is, exit the loop.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-first-occurrence-of-one-list-in-another
First Occurrence of One List in Another in Python
When it is required to find the first occurrence of one list in another list, the โ€˜setโ€™ attribute and the โ€˜nextโ€™ method is used. Example Below is a demonstration of the same
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.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ help finding first instance of a element in tuple/list
r/learnpython on Reddit: Help finding first instance of a element in tuple/list
October 13, 2022 -

Hello everyone, I'm currently working with graphs on python and I'm trying to implement a program for school where I can find all the roads to certain nodes and print the node found first. I'm stuck on this last part specifically. After doing my function I finally get my roadmap as such:

([[5], [4, 5], [4], [3, 4], [3], [1, 3], [1], [3, 7], [7], [1, 7], [1, 8], [8], [2, 7], [2], [2, 6], [6]],)

Where I was looking for the roads starting from 5 to 6,7 and 8. As you can see here I found [7] first.

My question is : how do I print only the first value found out of a list of 3 or more numbers? In this case in particular it would be 7 (notice I only want the instance where 7 is alone, not [3,7] or [1,7]

I already tried:

item for item in tuple if item==7 or item==8 or item==9

to no avail

Thank you!

Top answer
1 of 2
2
You have a weird data format. Your line a = ([[5], [4, 5], [4], [3, 4], [3], [1, 3], [1], [3, 7], [7], [1, 7], [1, 8], [8], [2, 7], [2], [2, 6], [6]],) Is actually a tuple containing 2 items. A list containing lists containing integers, None all because of that comma at the end! So if we call that tuple a: answer = [item for item in a[0] if item in [[7],[8],[9]]]
2 of 2
2
To look at just the first item in a tuple, you can use item[0]. Ie given the above list: [item for item in lst if item[0] in [5,6,7,8]] Will get you all items where the first value is 5, 6, 7 or 8. (You could also write it as item[0] == 5 or item[0] == 6 or ... as you did, but using an in check is more convenient for things like that.) There's a slight complication if your list here can have empty values, since item[0] on an empty list will raise an exception. If you need to handle that case, you could do something like: [item for item in lst if item and item[0] in [5,6,7,8]] (The if item will guard against empty items, so it'll never go on to the next check and try to access the first item) I already tried: item for item in tuple if item==7 or item==8 or item==9 This is failing because item isn't 7, it's a list that may start with 7. If you were just looking for 7 on its own etc, you could do if item == [7]. Also, just to check: your tuple here actually has an extra level of nesting compared to this check. Ie. it's a tuple containing a single list, which contains lists of numbers - above I've been assuming you're iterating over that inner list, not the outer tuple.