Your logic is wrong because you are returning a counter if i==x , and you have an extra loop at the end of your function.
Instead, you iterate over the inverse listing forms of your list and return the index of the first occurrence:
def PositionLast (x,s): return next(i for i,j in list(enumerate(s))[::-1] if j == x)
Demo:
print PositionLast (2, [2,5,2,3,5,3]) 2 print PositionLast (3, [2,5,2,3,5,3]) 5 print PositionLast (5, [2,5,2,3,5,3]) 4
source share