FAQ: Code Challenge: Loops - Reversed List


This community-built FAQ covers the “Reversed List” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Reversed List

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


I just want to make a comment: currently, if the code doesn’t produce the expected output, the only result is a message at the bottom of the window. This means that if I create a print statement to help try and diagnose what the code is doing (in order to fix the problem), the output of the print statement is not shown. Please change the functionality of the code environment to better accommodate user-driven debugging, rather than only providing pre-determined feedback. As-is, I have to guess what might be going wrong until it works.


This code isn’t making any sense to me. Here’s what I’ve got:

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

Here’s my problem:
when I have the second print() run through my code, it shows up as true.
The two lists are lst1 = [1, 5, 3] and lst2 = [3, 2, 1]

So in theory, on the second run-through of the loop, it’s doing this:
if lst1[1] == lst2[-1 - 1]:
return True
return False

I know that lst1[1] is equal to 5 and lst2[-1 -1] is lst2[-2] which is equal to 2. These are clearly not equal, yet I’m getting a True return.

Can someone explain why this is?


unfortunately, your code never gets to the second iteration/run through of the loop

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends


I think I understand. So I only want to stop the function by returning if the return gives the answer I’m looking for. Otherwise the return needs to function outside of the loop?


The moment you determine list b isn’t the reverse of list a, you could return False. You don’t have too make the remaining iterations, given you already know what you need to know


You should return when you’re done. Doing it any earlier makes no sense at all and doing it any later is pointless.


here its alright if i do this, the new value of lst2_new comes out just fine. but then i put the full code
"def reversed_list(lst1,lst2):
while len(lst1)>len(lst2_new):
if lst2_new == lst1:
return True
return False "
it doesnt work.


see it doesnt work, why???


I didn t understand why
lst2[len(lst2) - 1 - index] would reflect the reverse of the indexes, in base of which logic reason?
thank you for your help


len(lst2) - 1 will give you the right most value, by increasing index variable you will go from right to left over the list


if anyone could post the official solution here I would appreciate it.
The following supposedly “solves it” but it still doesn’t solve for reversed_list(, )

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

#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]))
print(reversed_list(, ))


We cannot see the indentation, but if the return statements are inside the loop, then the program does not run to completion.

Do we really need a nested loop? Can we do this with a single loop? You have the idea, already to use negative indices on one list.

s = [0, 1, 2, 3, 4, 5, 6]

Given the above list, let i equal 0 and let j equal -i - 1

i == 0; j == -1
s[i] == 0
s[j] == 6

Increment i and compute j.

i == 1; j == -2
s[i] == 1
s[j] == 5

Increment i and compute j.

i == 2; j == -3
s[i] == 2
s[j] == 4

and so on to `i == 5; j == -6.

What this means is we can iterate over one list and compare lst1[i] to lst2[j]. If there is no match, return False, else do nothing (so no else clause). Once the loop completes a full run, then and only then, return True.


Thank you very much.


Why is this not correct? If I print lst1 lst2 they look the same…

def reversed_list(lst1, lst2):
    if lst1 == lst2.reverse():
      return True
    	return False


.reverse() modifies the original list, and returns None.


Still don’t get this…

lst1[index] != lst2[len(lst2) - 1 - index]:

# I struggle to understand what it means:
lst1[index]  - # starts at 0 ?
lst2[len(lst2) - 1 - index #I read as starts at 2 - 1 - index 


Write down some word on a piece of paper in big letters.
Point your left hand at the first letter
Point your right hand at the last letter
Compare what you’re pointing at
Move both hands towards the other end of the word

Alternatively, READ the code. Evaluate in your head what happens and follow along, observing.


tnx; did that and run it trough the vscode debugger.

it’s something like this:

3 - 1 - 0 = position : 2 = <number 1>
3 - 1 - 1 = position : 1 = <number 2>
3 - 1 - 2 = position : 0 = <number 3>

makes sense now…

still don’t know why I can’t use reverse()


i covered this:

None will not equal lst1