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 ย  14 hours ago
Discussions

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
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
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
Help with Find the Index of the First Occurrence in a String
Time complexity usually refers to the worst-case time complexity (unless specified otherwise), so this solution is still in O(M * N). More on reddit.com
๐ŸŒ r/leetcode
2
1
March 3, 2023
๐ŸŒ
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.
๐ŸŒ
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:
๐ŸŒ
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)
๐ŸŒ
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)
๐ŸŒ
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.
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 - from collections import deque # Initializing lists a = [1, 2, 3, 4, 5, 6] b = [3, 4, 5] # Creating a deque for comparison window = deque(a[:len(b)], maxlen=len(b)) index = 0 if list(window) == b else -1 for i in range(len(b), len(a)): window.append(a[i]) if list(window) == b: index = i - len(b) + 1 break print(index) ... A deque is initialized with the first len(b) elements. The deque slides over a, checking for a match with b. ... Sometimes, while working with Python, we can have a problem in which we need to get occurrence of 1 element in another.
๐ŸŒ
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.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-get-index-of-item-in-list
Python Get Index of Item In List | 3 Simple Methods
June 29, 2024 - Pythonโ€™s list.index() function is a built-in method that returns the index of the first occurrence of an item in a list.
๐ŸŒ
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. ... The list.index() method returns an integer that represents the index of first occurrence of specified element in the 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:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-index-find-index-of-element-in-list
Python Index โ€“ How to Find the Index of an Element in a List
May 2, 2022 - We can see the problem here: using just index() will give the first occurrence of the item in the list โ€“ but we want to know the index of "Math" after shelf 4.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ list โ€บ index
Python List index()
Note: The index() method only returns the first occurrence of the matching element.
๐ŸŒ
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....
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_finding_the_index_list_item.htm
Python Finding the Index of a List Item
Python TechnologiesDatabasesComputer ... QualityManagement Tutorials View All Categories ... The index() method of list class returns the index of first occurrence of the given item....