Why couldnt you use a == sign to check wether the lists are reversed?

for the following assignment:
Create a function named reversed_list() that takes two lists of the same size as parameters named lst1 and lst2 .
The function should return True if lst1 is the same as lst2 reversed. The function should return False otherwise.
For example, reversed_list([1, 2, 3], [3, 2, 1]) should return True .

solution

def reversed_list(lst1,lst2):
  for index in range(len(lst1)):
   if lst1[index] != lst2[len(lst2) - index - 1]:
     return False
  return True 
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
  1. when I try to check wether the lists are reversed except to give a true statement when the condition is met instead of checking wether its false it gives me 2 true statements while the conditions arent met for the second list.
  2. why doesnt an else work for the second return command?

thanks in advance!

Hi :slight_smile: ,

When pasting code in the future remember to use the </> button to format it. Especially an example like this can get really ambiguous without proper indentation.

I’ll answer backwards:

    1. You logically don’t need an else command since the first return statement would terminate the function anyway if it ran. So the logic already behaves as if there were an if. It’s ok if you put it, it’s just an explicit way to do the same thing.
    1. What do you mean by this statement? Try giving an example of what the code looks like in your scenario and what the output is.
1 Like

thanks so much for your response!
the only difference was that the != would be an ==, and the false and true statement swapped. so it would print true if the condition was met, otherwise return false. I found the output however to be true for both in this case, while for the second printed reversed list the indexes didnt match in reverse

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

Let’s consider the nature of the comparison logic. Equality needs to be strictly equal to be valid, so anything that breaks it should be an immediate flag, and the function can end.

If we look at the opposite, partial equality is incomplete information, we can’t end the function just because a partial condition is met. We would need some sort of tracker in this case (not ideal for this type of function).

Here’s a possible way to debug though, which I find useful when I get too close to my own code:

def reversed_list(lst1,lst2):
  for index in range(len(lst1)):
    lead_var = lst1[index]
    trail_var = lst2[len(lst2) - index - 1]
    print("comparing {} from lst1 with {} from lst2".format(lead_var, trail_var))
    if lead_var == trail_var:
       return True
  return False

Let me know if this clarifies it!

hmm but dont we want the function to end if the condition is not met, since it found the lists not to be reversed?

also in what way the information be incomplete in the case that the we code it like i tried. it did return a true statement eventhough the middle indexes arent equal

Yes, that’s why

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

this doesn’t work. Because it ends when only a partial condition is met.

i realize i dont understand what is partial about it, what would be a complete condition in this case?

Well, try running the example I gave.

If every element were checked, you’d see the print statement 3 times. But since it the condition for returning is the equality of a single statement, it stops in the first place of the loop.

What this version of the function actually does is check, is at least one of the elements in a reversed list the same:

def is_at_least_one_the_same(lst1,lst2):
  for index in range(len(lst1)):
    lead_var = lst1[index]
    trail_var = lst2[len(lst2) - index - 1]
    print("comparing {} from lst1 with {} from lst2".format(lead_var, trail_var))
    if lead_var == trail_var:
       return True
  return False

that’s why the given solution uses != instead of ==

it still gave 2 true statements hmm

Right, that’s the wrong way to do it I’m saying (or rather, it achieves a different goal :slight_smile: ). You should not check for == but for != (the solution provides != correctly).

1 Like