Trying to find another way (that makes better sense to me) to solve “Question #5. Reversed List” in Python Code Challenges: Loops (Advanced). See link below …
The provided hint suggests that we create the code thinking of it this way: “Let’s say the lists are of size 5. You want to compare lst1[0] with lst2[4] , lst1[1] with lst2[3] and so on.”
But my initial hope of creating the code was to think of it this way: " Let’s say the lists are of size 5. You want to compare lst1[0] with lst2[-1] , lst1[1] with lst2[-2] and so on."
Do you know what I mean? What would the code solution be if we created it based on my way of thinking? I’m sitting here with writer’s block (aka coder’s block). HELP. See the incomplete skeleton of my code below …
def reversed_list(lst1, lst2):
–>for index in range(len(lst1)):
---->if lst1[index] != #HELP HERE PLEASE FILL IN MY BLANK:
------>return False
–>return True
Thank you janbazan for your response to this question.
Yes I understand that the provided Codecademy solution lst1[index] with list2[-index-1] is derived from the thinking: “Let’s say the lists are of size 5. You want to compare lst1[0] with lst2[4] , lst1[1] with lst2[3] and so on.”
But I wanted to learn what the code solution would look like and how it would be different when the thinking is changed to: “Let’s say the lists are of size 5. You want to compare lst1[0] with lst2[-1] , lst1[1] with lst2[-2] and so on.” What would the math formula be if we’re thinking of lst2 from the back of the list … instead of from the front of the list?
There is no indent, apart from being in the function block, so that loop has a chance to run completely through. else is not needed because return is used.
Thought we first checked for the indices to match, and if not we return false, so it would be after (hence the intends) the if statement. But so I understand you correctly, we’re letting it run, see if it returns true, and then return True?