Return will end a function and stop any loops

This code isn’t making any sense to me. Here’s what I’ve got:

def reversed_list(lst1, lst2):
for i in range(len(lst1)):
if lst1[i] == lst2[(-i - 1)]:
return True
return False

Here’s my problem:
when I have the second print() run through my code, it shows up as true.
The two lists are lst1 = [1, 5, 3] and lst2 = [3, 2, 1]

So in theory, on the second run-through of the loop, it’s doing this:
if lst1[1] == lst2[-1 - 1]:
return True
return False

I know that lst1[1] is equal to 5 and lst2[-1 -1] is lst2[-2] which is equal to 2. These are clearly not equal, yet I’m getting a True return.

Can someone explain why this is?

1 Like

30 posts were merged into an existing topic: Did you double check the placement of return?