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
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 › 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   2 days ago
Discussions

How can I find a value in a list using Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
I currently use the following code to check if an item is in my_list: if item in my_list: print("Desired item is in the list") Is using if item in my_list: the most “pythonic” way to find an item in a list? More on community.testmuai.com
🌐 community.testmuai.com
0
June 3, 2024
Second occurrence index number from list.
I think its not well known that the list.index method takes optional start and stop arguments: >>> help(list.index) Help on method_descriptor: index(self, value, start=0, stop=9223372036854775807, /) Return first index of value. Raises ValueError if the value is not present. So perhaps the simplest way to accomplish this would be something like: nums = [1, 2, 3, 4, 1, 2, 3, 4] first = nums.index(2) second = nums.index(2, first + 1) print(nums[:second]) More on reddit.com
🌐 r/learnpython
16
3
March 15, 2022
python - How to return the first index occurence of item in lists? - Stack Overflow
Learning Python and tasked with returning the index location of the first letter in the lists. But it has to be to the left uppermost part on any given list. For example 'a' would return as index (... More on stackoverflow.com
🌐 stackoverflow.com
Index repeating elements in a list
(I turn both words into a list to check each character.) That's probably unnecessary, since strings and lists share a lot of methods, unless you're mutating them. But it doesn't sound like you are. The problem is that if I enter a word like 'these', it will say both E's are in the right position since index stops at the first occurrence. Does anyone know I could fix this? So why not loop over the correct answer and check the characters one by one? Something like this: right_pos = 0 for idx, char in enumerate(word): if char == guess[idx]: right_pos += 1 I could've used sum and a generator expression, but figured this is easier to understand. More on reddit.com
🌐 r/learnpython
10
3
September 23, 2022
🌐
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.
🌐
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)
🌐
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)
🌐
W3Schools
w3schools.com › python › ref_list_index.asp
Python List index() Method
Python Examples Python Compiler ... Interview Q&A Python Bootcamp Python Training ... The index() method returns the position at the first occurrence of the specified value....
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list
Python Find in List – How to Find the Index of an Item or Element in a List
February 24, 2022 - Although the start and end parameters provide a range of positions for your search, the return value when using the index() method is still only the first occurence of the item 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: try: print(fruits[10]) ## This will raise an IndexError except IndexError as e: print("Index out of range!") ... At LabEx, we recommend mastering these fundamental indexing techniques to become proficient in Python list manipulation. The most straightforward way to find the first occurrence:
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index()
Note: The index() method only returns the first occurrence of the matching element.
🌐
Built In
builtin.com › software-engineering-perspectives › python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
The index() method in Python finds the first occurrence of a specific character or element in a string or list. If the character or element is found, its index value will be returned.
🌐
TestMu AI
community.testmuai.com › ask a question
How can I find a value in a list using Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
June 3, 2024 - I currently use the following code to check if an item is in my_list: if item in my_list: print("Desired item is in the list") Is using if item in my_list: the most “pythonic” way to find an item in a list?
🌐
Real Python
realpython.com › python-first-match
How to Get the First Match From a Python List or Iterable – Real Python
May 28, 2023 - For example, you want to find a name in a list of names or a substring inside a string. In these cases, you’re best off using the in operator. However, there are many use cases when you may want to look for items with specific properties. For instance, you may need to: ... 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:
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-find-the-index-of-the-first-occurrence-of-the-specified-item-in-the-array
Python Program to find the index of the first occurrence ...
May 29, 2023 - element : It can be int, float, string, double etc. start(optional) : start searching an element from that particular index. stop(optional) : stop searching an element at that particula index. Following is the basic example of Python array index method − · import array as arr my_arr1 = arr.array('i',[13,32,52,22,3,10,22,45,39,22]) x = 22 index =my_arr1.index(x) print("The index of the element",x,":",index)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-find-index-containing-string-in-list
Python - Find Index containing String in List - GeeksforGeeks
July 23, 2025 - index() method in Python is used to find the position of a specific string in a list. It returns the index of the first occurrence of the string, raising it ValueError if the string is not found.
🌐
Codecademy
codecademy.com › docs › python › lists › .index()
Python | Lists | .index() | Codecademy
June 11, 2025 - The .index() method is a built-in Python list method that returns the index position of the first occurrence of a specified element within a list.
🌐
W3Schools
w3schools.com › python › ref_string_index.asp
Python String index() Method
Remove List Duplicates Reverse ... Python Interview Q&A Python Bootcamp Python Training ... The index() method finds the first occurrence of the specified value....
🌐
FavTutor
favtutor.com › blogs › get-list-index-python
Python Find String Position in List (Get the Index of an Item)
1 day ago - In Python, you get the index of an element in a list with the index() method: my_list.index(value) returns the position of the first match.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-find-the-index-for-a-given-item-in-a-python-list
How to Find Index of Item in Python List - GeeksforGeeks
December 26, 2024 - Whether we’re checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list item. index() method returns the index of first ...