Looking at your answer, if the first two elements compared are equal the remaining elements of each list will never be checked. What does the return statement do?
thanks! idk if i got it right though… i thought the loop would run until len(lst1) is reached… does the True statement work like a break and thats why i have to try the false first?
return ends the execution of a function (EVERY function EVERY time) it is executed. When your first comparison evaluates to True, your code returns True to the line of code that called the function, so the remaining elements are not iterated through at all. You never want a return statement to be reached until the function has completed the task assigned to it. You could check the equality of the elements rather than the inequality if you really wanted to, but it would require a few more lines of code, and changing the return values around:
def reversed_list(lst1, lst2):
for i in range(len(lst1)):
if lst1[i] == lst2[len(lst1)-1-i]:
continue # if they're equal do nothing
else:
return False # if they're not equal return False (this ends the function)
return True # if we get this far, the entire lists have been compared, and we haven't returned False, so we can safely return True
As you can see, comparing the inequality as the solution suggested makes the code more concise, and perhaps easier to follow.
Don’t feel bad. Many, many new coders are confused by what exactly return does. Hope this helps!
Happy coding!!