Stuck on Python Code Challenges: Loops (Advanced) 5. Reversed List

I am a bit stuck on 5. Reversed List on this page of advanced challenges

I can see the suggested answer and understand it. However, I came up with a different solution that seems to get the same results and I’m not sure why it isn’t being marked as correct.

The code I tried was:

#Write your function here
def reversed_list(lst1, lst2):
  i = -1
  for num in lst1:
    if num != lst2[i]:
      return False
    elif i == (0 - len(lst1)) and num == lst2[i]:
      return True
    else:
      i -= 1

#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]))

It seems to get the correct answers, and the logic makes sense to me. However, when I try to check my work, I just get the message:

reversed_list([], []) should have returned True , and it returned None

I’m not sure what this error means because it did return True and False as far as I can tell. I don’t see it ever returning None? So any advice would be appreciated.

It does give you the answer. print(reversed_list([], [])) does return None.

The reason is, well, in another language you might get a compile or syntan error of “not all code paths return…”. Python will, helpfully(?!?), implicitly return a None if you don’t return anything. Let’s make that explicit so you can see what’s going on:

#Write your function here
def reversed_list(lst1, lst2):
    i = -1
    for num in lst1:
        if num != lst2[i]:
            return False
        elif i == (0 - len(lst1)) and num == lst2[i]:
            return True
        else:
            i -= 1
    return None # if you don't return inside that loop, you land here

#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]))
print(reversed_list([], []))

I’ll leave it to you to impliment a fix.

Forgive me, but this looked like fun. Another solution might be:

def reversed_list(xs, ys):
    sz = len(xs)
    if sz != len(ys): # no need to check
        return False
    if sz == 0: # no need to loop
        return True 
    for i in range(sz):
        if xs[i] != ys[sz - 1 - i]:
            return False
    return True