How to find the last occurrence of an item in a Python list

If you are actually using just single letters like shown in your example, then str.rindex would work handily. This raises a ValueError if there is no such item, the same error class as list.index would raise. Demo: >>> li = [“a”, “b”, “a”, “c”, “x”, “d”, “a”, “6”] >>> ”.join(li).rindex(‘a’) 6 For the more general … Read more