Use .rfind():

>>> s = 'hello'
>>> s.rfind('l')
3

Also don't use str as variable name or you'll shadow the built-in str().

Answer from Rik Poggi on Stack Overflow
🌐
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.

🌐
Tutorialspoint
tutorialspoint.com › home › python › python string rindex method
Python String rindex Method Explained
February 21, 2009 - Python string method rindex() searches for the last index where the given substring str is found in an original string. This method raises an exception if no such index exists, optionally restricting the search to string length, i.e. from the ...
🌐
W3Schools
w3schools.com › python › ref_string_rfind.asp
Python String rfind() Method
Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types
🌐
Inductive Automation
forum.inductiveautomation.com › ignition
Vision Scripting: IndexOf and LastIndexOf / Substring - Ignition - Inductive Automation Forum
January 31, 2023 - Trying to write a script that breaks apart a string. I realize I can't use the expression functions, but I can't seem to figure out how to find the index of a colon in a string, and then take a substring up to that index. I've found some previous posts about how to do substrings, but they are all using literal values, and I need to substitute a variable using something equivalent to IndexOf and LastIndexOf.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-find-last-occurrence-of-substring
Python | Find last occurrence of substring - GeeksforGeeks
July 11, 2025 - # Python3 code to demonstrate # Find last occurrence of substring # using rindex() # initializing string test_string = "GfG is best for CS and also best for Learning" # initializing target word tar_word = "best" # printing original string print("The original string : " + str(test_string)) # using rindex() # Find last occurrence of substring res = test_string.rindex(tar_word) # print result print("Index of last occurrence of substring is : " + str(res))
Find elsewhere
Top answer
1 of 11
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 11
51

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.

🌐
IQCode
iqcode.com › code › python › lastindexof-python
lastindexof python Code Example
how to access last element from negetive indexing in python last index of list in python last index numberof the list in python get items from list python between 3 and last index how to check last index python python lastIndexOf python last index of last index of in string python how to get the index of the last element inpython how to print how the last index in python last index of function in python how to get 10 last index python lastindexof python last index of a list in python str index last last index valueof python last value index python last index of element search in list python how to find last index in python how to assign last index to a variable in python find the last index of array in python last index of array in python last index occurence python python list find last index < 2 Which index is the last element in a list called nums at?
🌐
Reddit
reddit.com › r/learnpython › how to get what index position the last character of a string is
r/learnpython on Reddit: how to get what index position the last character of a string is
April 2, 2022 -

Hey. I'm trying to get the index position of the last character in a string. For example, if the string is "Hello" , I want the program to only print that the last character (in this case 'o') is in index position 4. The string would be inputted by the user though so its always going to be different.

I'm familiar with (len(string)) and string[-1] but not sure how to use them together, if I even need to.

🌐
Programiz
programiz.com › javascript › library › string › lastindexof
JavaScript String lastIndexOf() (With Examples)
Here, we have passed "Python" as a substr. Since "Python" is not found in the "I love JavaScript" string, the method returns -1. The lastIndexOf() method is case sensitive.
🌐
Sentry
sentry.io › sentry answers › python › get the last element of a list in python
Get the last element of a list in Python | Sentry
The Problem How do I get the last element of a list in Python? The Solution In Python, we can get the last element of a list using index notation. A positive…
🌐
Jessica Temporal
jtemporal.com › the-last-of-a-list-in-python
The last of the list with Python | Jessica Temporal
September 11, 2023 - 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. And that’s ok! It works. However, Python presents a more elegant way of doing this, see:
🌐
W3Schools
w3schools.com › python › ref_string_rindex.asp
Python String rindex() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
W3Schools
w3schools.com › java › ref_string_lastindexof.asp
Java String lastIndexOf() Method
public class Main { public static void main(String[] args) { String myStr = "Hello planet earth, you are a great planet."; System.out.println(myStr.lastIndexOf("e", 5)); } } ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Pericror
pericror.com › software-qa › how-to-javascript-array-lastindexof-in-python
How to Javascript Array lastIndexOf in Python
June 18, 2023 - By using the `index` method in ... the equivalent function of Javascript’s Array lastIndexOf in Python is the rindex() method....
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-last-index-character-string
Find last index of a character in a string - GeeksforGeeks
July 27, 2022 - # A Python program to find last # index of character x in given # string. # Returns last index of x if it # is present. Else returns -1.
🌐
W3Schools
w3schools.com › jsref › jsref_lastindexof.asp
JavaScript String lastIndexOf() Method
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string.