Help with Advanced Loop challenge

I am working on the last problem of the code challenges for loops, in which you have to write code that compares the first list to the reverse of the second list. My code appears to return False every time it is called. Can someone tell me why? Why does my third line not compare the first list to the reverse of the second list?

#Write your function here def reversed_list(lst1, lst2): if lst1 == lst2.sort(reverse = True): return True else: return False #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 like the issue is a slight misunderstanding of how sort() works.
When calling sort on a list, the list gets sorted in ascending order meaning that the lower numbers go first and are followed by progressively higher numbers:

list1 = [1,4,2]
print(list1) #prints [1,2,4]

The optional “reverse=True” reverses this sorting so that the list gets sorted in descending order:

list1 = [1,4,2]
print(list1) #prints [4,2,1]

Pay attention to the fact that the list is sorted before It is used, the sort() method changes the list “in place” and does not have a return value.

print(list1.sort(reverse=True)) #prints None since sort() does not return a value

Now! What happens when we use a list that already Is sorted in descending order and sort it with the “reverse=True” parameter?

This should push you in the right direction to figure out what is going wrong, if needed you can print the lists separately to see what you are actually comparing!

Note Using sort() is also not the correct way to solve this challenge! reverse() on the other hand sounds like something that could prove useful :wink:

Happy coding and keep learning!

1 Like