Hello,
I’m working on the Loops (Advanced) Exercise 5: Reversed List.
Why does the solution provided work?
def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
return True
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
But this code does not work?
def reversed_list(lst1, lst2):
for vals in range(len(lst1)):
if lst1[vals] == lst2[len(lst2) - 1 - vals]:
return True
return False
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
The solution returns (as expected):
True
False
But the latter returns:
True
True