FAQ: Code Challenge: Loops - Reversed List

In the first example, if just one character of the “reversed” list is in the correct position, the function will return True, even if all of the rest are incorrect. In the false example, 1 in lst1 compares equal to 1 in lst2, and the function is done, returning True.

:arrow_right: Remember that once return is reached one time, the function returns its value and halts.

In the second example, a single incorrect value returns False, which (if you think about it) is what you want.

So I found a different solution, and wondering if there’s any reason this is an unreliable way to solve something like this.

I created an empty list called “checklist,” made the comparison of list indexes == instead of !=, and appended a placeholder item to “checklist” every time it was True. Then, to determine True or False, I compared len(checklist) to len(lst1), because theoretically if every comparison was equal, checklist would have the same amount of items (from being appended).

The method would take a complete iteration of the list, and would require an extra step to append the list, which is itself a data structure. Then the final comparison is an added step. That would amount to,

2 * N + 1 

steps, and a data structure.

The method that returns False on the first non-match uses only that many steps, and short-circuits the function. The maximum number of steps will be N.

Could someone explain to me why to following code is incorrect?
It’s supposed to return “True” and then “False”, but I get “True” and “True” with this code. I know it has to do with the ‘else:’ part and probably something with indentation, but I don’t understand what the problem is…


It will return the condition (match or non-match) of the first index tested, and then halt, since what return does two things:

  1. Return the value to the calling statement, and
  2. Halt processing of the function

In other words, once return is reached one time, the function is over.

Hi everyone!

Can anyone explain to me what is the difference between the two?
Why one is correct and the other is not?


return happens at the end of function, handing data back to the caller

which means, in the second case, the moment any value (lets say the first value) equals the opposite value (last value in the list), true is returned, the function stops. Which isn’t the desired behavior.

you only want to return true after comparing all values, not at the first match.

You can return False when values do not match, and not compare the rest of the values.

1 Like