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?
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] list1.sort() 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] list1.sort(reverse=True) 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
Happy coding and keep learning!