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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-last-occurrence-of-some-element-in-a-list
Last Occurrence of Some Element in a List - Python - GeeksforGeeks
July 11, 2025 - For example, w = ["apple", "banana", "orange", "apple", "grape"] we need to find last occurrence of string 'apple' so output will be 3 in this case. rindex() method in Python returns the index of the last occurrence of an element in a list.
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.

Discussions

python - How do I get the last element of a list? - Stack Overflow
Copy>>> empty_list[-1] Traceback ... list index out of range ยท But again, slicing for this purpose should only be done if you need: ... As a feature of Python, there is no inner scoping in a for loop. If you're performing a complete iteration over the list already, the last element will still ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to obtain the last index of a list? - Stack Overflow
This answer is sooo bad, how did ... is built into Python, much faster, and simpler? len(list1-1) is definitely NOT the way to go. 2023-04-25T16:48:38.647Z+00:00 ... @user1270710 The question was about the last index of the list not the last element.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why is the index -1 always denoted as the last element of a list in python?
You can count backwards from the end like this. -1 is the last item, and -2 is the second to last item, etc. More on reddit.com
๐ŸŒ r/learnpython
7
1
February 25, 2022
Looping until the second to last element in a list, what's best practice?
for item in the_list[:-1] More on reddit.com
๐ŸŒ r/learnpython
23
3
January 11, 2024
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ finding last index of some value in a list in python
r/learnpython on Reddit: Finding last index of some value in a list in Python
August 5, 2024 -

Let's say v is a list or a tuple.

To find the last occurrence of value in v we would need to compute len(v) - 1 - v[::-1].index(value)

Why is this? Why must we subtract the last term from len(v) - 1? Why does simply writing v[::-1].index(value) give the wrong result?

In fact, what does v[::-1] actually do? Doesn't it reverse the list/tuple? If it does reverse it, then v[::-1].index(value) should give the last occurrence of value in v, but for some reason it does not work like that.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-how-to-get-the-last-element-of-list
Get the Last Element of List in Python - GeeksforGeeks
July 11, 2025 - Explanation:len(a) - 1 gives the index of the last item, which is then used to retrieve the value. We can also find the last element by using len() function. Find length of the list and then subtracting one to get the index of the last element.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ get the last element of a list in python
Python Last Element in List Using Negative Indexing | Sentry
2 weeks ago - Access the last element of a Python list using negative index notation with [-1], which counts positions from the end of the list instead of the start
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ how-to-get-last-element-in-list-in-python
How do I get the last element of a list in Python? | Better Stack Community
January 26, 2023 - You can also use the len() function ... it to index the last element. For example: ... Both of these approaches will work regardless of the length of the list. ... This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike ...
Find elsewhere
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-get-last-element-in-list-how-to-select-the-last-item
Python Get Last Element in List โ€“ How to Select the Last Item
March 15, 2022 - In Python, we can also use negative values for these indexes. While the positive indexes start from 0 (which denotes the position of the first element), negative indexes start from -1, and this denotes the position of the last element.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-get-the-last-element-of-a-list-in-Python
How to get the last element of a list in Python?
August 23, 2023 - List slicing with [-1:] returns a new list containing only the last element. To get the element itself, access index [0] of the result.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ get last element of list in python
How to Get Last Element of List in Python | Delft Stack
February 15, 2024 - In the above code, we first initialize a list list1 and then get the last element of the list1 with the pop() function. If we do not want to remove the last element from the list, we have to use -1 as the list index.
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ get the last element of a list in python
Get the last element of a list in Python - PythonForBeginners.com
August 11, 2021 - In python, we can use positive indices as well as negative indices. Positive indices start with zero which corresponds to the first element of the list and the last element of the list is identified by the index โ€œlistLen-1โ€ where โ€œlistLenโ€ is the length of the list.
๐ŸŒ
How to Use Linux
howtouselinux.com โ€บ home โ€บ 3 ways to get last element of a list in python
3 Ways to Get Last Element of a List In Python - howtouselinux
October 9, 2025 - The first element in a list will ... sequence of objects in square brackets ([]). So, if we want to get the last element in a list, we can just use index -1....
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to get the last element of a python list?
How to Get the Last Element of a Python List? - Be on the Right Side of Change
September 27, 2020 - To access the last element of a Python list, use the indexing notation list[-1] with negative index -1 which points to the last list element. To access the second-, third-, and fourth-last elements, use the indices -2, -3, and -4. To access the n last elements of a list, use slicing list[:-n-1:-1] ...
๐ŸŒ
Jessica Temporal
jtemporal.com โ€บ the-last-of-a-list-in-python
Getting the last element of a list in Python | Jessica Temporal
1 month ago - Coming from other languages like ... more or less like this to get the last element of a list: Use the len() size function to get the length of the list and subtract 1 to get the index of the last element in that list...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-get-last-element-in-list
Python: Get Last Element in List
March 2, 2023 - lastElement = exampleList[-1] print("Last element: ", lastElement) print("exampleList: ", exampleList) secondToLast = exampleList[-2] print("Second to last element: ", secondToLast) ... Negative indexing does not change the original list. It is only a way of accessing elements without any changes to the original list. This is by far the simplest and most Pythonic solution, favored by most.
๐ŸŒ
YouTube
youtube.com โ€บ shorts โ€บ 8JqKYkW8DFo
What Is The Index Of The Last Element In A List Python - YouTube
November 25, 2025 - That video answers what is the index of the last element in a list Python. It shows off an example to answer the question.#python #shorts
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ last element in list python
Program to Get the Last Element in List in Python - Scaler Topics
May 18, 2023 - In Python, we can also use negative values to access the elements from the list. While the positive index starts from 0, which denotes the first element of the list, the negative index starts from -1 and denotes the last element of the list in Python.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ get-the-last-element-of-a-list
Here is how to get the last element of a list in Python
To get the last element of a list in Python, you can use the index -1.You can also use the pop() method to remove and return the last element of a list. This method removes the element from the list, so if you want to keep the original list intact, you can use the slicing syntax to get a copy ...