Did you double check the placement of `return`?

hello there please why in this exercice this code doesn’t work
def reversed_list(lst1, lst2):
for i in range(len(lst1)):
if lst1[i] == lst2[len(lst2)-1-i]:
return True
return False

The code does not confirm that one list is the reverse of the other one; it only returns a True on the first matched element. It is not a viable solution.

1 Like

I did it fairly differently as well. The site tells me it doesn’t work, but I’m getting the result I want, so I’m not sure what the problem is.

The first problem is that i is not an index, but a value.

The second problem is that return True is inside the loop. We don’t want to return on the first match, but rather the first non-match.

for i in range(len(lst1)):
    if lst1[i] == lst2[-(i + 1)]: continue
    else: return False
return True
>>> reversed_list([1, 5, 3], [1, 5, 3])
False
>>> reversed_list([1, 5, 3], [3, 5, 1])
True
>>> 

Shouldn’t you be testing with the input data it says it fails on?

Can some one break this down for me?
Why does this code satisfy the Reversed List challenge:

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

BUT not this:

def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
else:
return True
wrong
The second code set includes the else statement with the if statement. Why does it not work?

Looking at one value from each list isn’t sufficient information to determine that the rest matches as well

Hello! could anyone help me on this please?

I don’t understand why the order of False and True affects the result.

so in the left picture which I try to work out for hours…
it returns True for both example. Whereas the right picture it returns True and False which is right answer.

Can anyone explain how the order of them affect the result please?
It’s beyond my brain capability!!
Thank you!

this might help:

http://www.pythontutor.com/visualize.html#mode=edit

it allows you to “walk” through your code

in the left image, only the first item of lst1 is compared with the last item of lst2, then either true or false is returned.

once the function has returned data, the function is done. So return will break the loop

1 Like

ohhhhh right, Thank you for your prompt and kind reply!! <3

And thanks for the link as well, it’s such a cool tool!!

This worked script for Reversed List:
def reversed_list(lst1, lst2):
lst2 = lst2[::-1] # reversed lst2 … -> 1,2,3
for index in range(len(lst1)):
if lst1[index] != lst2[index]:
return False
return True

It looks like your new list is just going to keep adding the last last indicie of lst2 until your new list is the same length of lst1.

if you used .pop to remove the -1 index of lst2 after you have appended your new list, your code should take the next last index from from lst2

def reversed_list(lst1, lst2):
new_lst =
while len(lst1) > len(new_lst):
new_lst.append(lst2[-1])
lst2.pop()
if lst1 == new_lst:
return True
else:
return False

edit: cleaned up code to be easier to read

My Solution using the reverse():

def reversed_list(lst1, lst2):
  lst1.reverse()
  if lst1 == lst2:
    return True
  else:
    return False
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))

This answer is getting me halfway to understanding the problem I’ve been staring at for far too long.
I had tried it as
if lst1[index] == lst2[len(lst2) - 1 - index]:
return True
else:
return False
(i later flipped around the == to != and false/true
In every test or change I made - using a while loop or for loop - it ends the loop after the first iteration/comparison and declared everything True because it never gets to test the second numbers.
So, if we are testing conditions with the if/else statements why do we put the return outside of those tests? (other than that the return stops the loop)

We can perfectly use return False in the loop, when a set of numbers doesn’t equal, we have our answer, so then its fine to end the function “prematurely” using return.

now return True (That the lists are opposites/reversed of each other) is only determined after we looped over all the numbers, which is why we return True after the for loop

1 Like

You’re carrying out a bunch of actions.
Given what you’ve done so far, is this a good time to quit the function?

This is something you can carry out using pen and paper. So arguably you already know what should happen when.

If you find what you’re looking for, then you’re done, right? And if you don’t, then you’d keep looking.

In particular what I want to point out here is that you already possess the answer, but you’re disconnecting that from your code somehow. Don’t. You have something to refer to, it’s not different, use it.

1 Like

Thanks for the response. It’s interesting to see how return in different places affects the code. I kept going back and putting print statements to test and see where the code was stopping or continuing. So, i was scratching my head thinking "i know I want it to run through the full loop to test each index but why is it stopping- better yet, how do I get it to NOT stop at the first iteration of the if statement. " This led me to adding an additional loop condition among other failed attempts.

In a later lesson under strings this came up again.
It’s explained pretty well in that instance here Why doesn't `else: return False` work?
AND this link is awesome for helping you literally see the steps the code takes if you were to shift the return indentation.

exiting at different points in time will have different outcomes. this is not surprising! you’re almost suggesting that you can scramble the order of operations and get the same outcome

changing indentation isn’t a difference in text with surprising outcome. did you, or did you not, mean to do something as part of a loop? you’d either put that something in the loop, or not.

some languages use curly braces to mark the beginning and end of loops/functions/whatever

for x in stuff {
  // this is inside the loop
}
// this is outside the loop

would it be surprising if you moved something out of or into those braces?
well, there’s no difference whatsoever, those braces are not adding any information, it’s entirely cosmetic

for x in stuff
  // this is inside the loop
// this is outside the loop
1 Like

What I meant there is the opposite - Depending on the placement of indent it has totally different outcomes. As someone learning this and finally understanding that - it is interesting to see it in action. Understanding that gives much better context to the in the loop out of the loop workings.