Suppose we have this function:
def search(ls, e):
'''Assumes ls is a list.
Returns True if e is in ls, False otherwise.'''
for i in reversed(range(len(ls))):
if ls[i] == e:
return True
return False
I'm trying to get this function to search through ls
starting from the end of the list instead of the beginning. Hence, I included reversed()
, which reverses the list, ls
.
However, what I'm specifically trying to do is search a list but instead of starting from the index of 0, starting from the index of -1. I don't think reversed()
technically does this? If not, how could the function be modified to achieve my objective?