FAQ: Code Challenge: Loops - Reversed List


#1

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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!


#2

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.


#3

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
else:
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
else:
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?


#4

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


#5

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?


#6

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


#7

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


#8


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):
lst2_new=
while len(lst1)>len(lst2_new):
lst2_new.append(lst2[-1])
lst2=lst2[0:-1]
if lst2_new == lst1:
return True
else:
return False "
it doesnt work.


#9


see it doesnt work, why???


#10

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


#11

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


#12

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
else:
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(, ))


#13

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.


#14

Thank you very much.