 Objective: test two lists to see if the second list is the reverse of the first list.

Here is the official solution:

def reversed_list(lst1, lst2): for index in range(len(lst1)): if lst1[index] != lst2[len(lst2) - 1 - index]: return False return True #Uncomment the lines below when your function is done print(reversed_list([1, 2, 3], [3, 2, 1])) print(reversed_list([1, 5, 3], [3, 2, 1]))

… but my code has a minor change that seems to make the code output incorrect.
I simply changed the logic from:

if lst1[index] != lst2[len(lst2) - 1 - index]:
return False

to

if lst1[index] == lst2[len(lst2) - 1 - index]:
return True

The Output is True for both lists, which is incorrect! Why am I not able to simple reverse the logic from != return False to == return True?

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

The important change is the placement of return. Remember that as soon as return is executed the function finishes up and provides that value back to the caller (nothing further in the function will run).

Following the logic through with the arguments you use in each call (pen and paper or otherwise) and you’ll likely spot the issue once you know how return works.

ok, for the second test list: print(reversed_list([1, 5, 3], [3, 2, 1]))

logic: if lst1[index] == lst2[len(lst2) - 1 - index]: return True

loop 1:

if lst1 == lst2[3-1-0] or lst == lst2
plugging in the values, 1 == 1
return True

loop 2:

if lst == lst2[3 -1 - 1] or lst == lst
plugging in the values at those indices, 5 == 2
Not True,
return False

Are you hinting that a break should be included following the return False?

return True

needs else: to tell python3 code this is the alternate thing to do and produce the desired result.

else: return True

def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
else: return True         <------- here

Hmmm… How does the loop finish? All that is really needed for that to work is return True after the loop completes.

1 Like

lol thanks mtf. I see what you mean

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

mtf reminded us all with a hint that in python placement of keywords and code is important to how the code performs.

That is still inside the loop. Outdent it to line up with the for.

? if I line it up with for instead of if, the second test returns false though.

That’s what it should return. If the return is inside the loop it only iterates but once, and returns a false positive.

1 Like

So, mtf, if you look at my original question, why is it when i replace the logic with == instead of !=
… and i reverse the Boolean logic to reflect the change (return True in the if loop and return False in the for loop),
the second function call returns True instead of the expected False?
If you look above (in response to tgrtim), I wrote out how I believe the loop should have returned False.
Where did I go wrong in my written expected execution of the code?

Sorry, I need you to bring me back up to speed. Please show us the code again, and what you expect it to do.