Possible Duplicate:
Finding first and last index of some value in a list in Python
Hi I was wondering if someone could help me with Python. I am trying to create a code that returns the last index of the last occurrence of an item in a list in a recursive way. So in a list [1,2,3,4,5,2]
the last it should return 4
. It only takes in 2 variables which are the list and the item that it is searching for. If it does not find any matching variable then it returns -1
.
So far I have this:
def lstIndex(lst, item):
if len(lst) == 0:
return -1
place = lst[0]
if place == item:
print(place)
#just return the index
return lst.index(place)
else:
return lstIndex(lst[1:],item)