# Question #5: Python Code Challenges: Loops (Advanced)

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` with `lst2` , `lst1` with `lst2` 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` with `lst2[-1]` , `lst1` 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

comparing
`lst1` with `lst2[-1]`
`lst1` with `lst2[-2]`
`lst1` with `lst2[-3]`

I think what you’re looking for is
`lst1[index]` with `list2[-index-1]`

1 Like

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 with lst2 , lst1 with lst2 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` with `lst2[-1]` , `lst1` 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?

I’m on the same challenge at the moment. This is what I currently have:

``````def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[(len(lst2) - 1 - index)]:
return False``````

Which gave me the correct answer on one of the questions. My instinct would be to continue like this:

``````def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[(len(lst2) - 1 - index)]:
return False
elif: lst1[index] == lst2[(len(lst2) - 1 - index)]:
return True``````

Which is obviously not giving me the answer I want. It makes both statements in the task True.

So looking at the solution, and dont understand why therese no “else” or why there’s no indent?

``````def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
return True``````

vs.

``````def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
else:
return True``````

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?

If the loop runs all the way through, then everything matches and the loop ends. The next line is the default return, True.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.