Indentifying the LAST occurrence of an item in a list

tkpmep at hotmail.com tkpmep at hotmail.com
Wed Apr 4 11:58:49 EDT 2007


For any list x, x.index(item) returns the index of the FIRST
occurrence of the item in x. Is there a simple way to identify the
LAST occurrence of an item in a list? My solution feels complex -
reverse the list, look for the first occurence of the item in the
reversed list, and then subtract its index from the length of the list
- 1, i.e.

LastOcc = len(x) - 1 - x[::-1].index(item)

Is there a simpler solution?

Thanks in advance

Thomas Philips




More information about the Python-list mailing list