FAQ: Code Challenge: Loops - Reversed List

3 posts were merged into an existing topic: How can I iterate over a list backward?

12 posts were merged into an existing topic: Did you double check the placement of return?

3 posts were split to a new topic: Why doesn’t this code work?

5 posts were merged into an existing topic: Did you double check the placement of return?

2 posts were split to a new topic: Why don’t we use else?

10 posts were merged into an existing topic: Could I solve this using reverse()? Is it efficient?

2 posts were merged into an existing topic: Did you double check the placement of return?

3 posts were merged into an existing topic: Did you double check the placement of return?

5 posts were split to a new topic: Boredless tourist question?

2 posts were merged into an existing topic: Did you double check the placement of return?

A post was split to a new topic: The next lesson won’t load?

3 posts were merged into an existing topic: Did you double check the placement of return?

A post was merged into an existing topic: Sharing solutions

I don’t understand the logic here, can someone explain why my code is wrong?
Thanks a lot!

def reversed_list(lst1, lst2):
  
  #lst1[0] = lst2[-1]
  #lst1[1] = lst2[-2]
  #lst1[2] = lst2[-3]
  
  for index in range(len(lst1)):
    for index in range(len(lst2)):
      if (lst1[index] == lst2[-1-index]):
        return True
      else:
        return False

its explained here:

Did you double check the placement of `return`?

and here:

Did you double check the placement of `return`?

These replies where part of this topic, but where splitted

Thanks a lot!! Also for the link!

I have no idea what’s going on in a solution :smiley: I did it my way, which, to me obviously seems easier and more elegant:

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

Can anyone talk me throught what;s going on in the solution? Why even use len? The whole for index in range(len(lst1)) doesn’t make sense to me. As I understand it it creates a set of numbers from 0 to the last index of lst1 but I don’t see how the code uses those numbers to iterate through lst1.
It doesn’t say “for every element of list lst1 do this:”. It says “for numbers from 0 to 2”. How the ■■■■ the code knows that we mean to use those numbers to iterate through elements of a list?

wrong question :wink: It will happen that you run into code samples (a project you inherit from another developer, code samples on stackoverflow) which you don’t understand. So what you really should learn is how to figure out what those code samples/function do.

How could we apply this here? We could use print() to see the values, this should give use some insight into how the code works

Or you could use this tool:

Python Tutor code visualizer: Visualize code in Python, JavaScript, C, C++, and Java

which will step through the code for you, allowing you to see the state of the program at every step (For this purpose you can also use debugger, this website is just easier give it requires no setup)

Or break the exercise solution down into small bits, figure out what they all do

Could i tell you how the proposed solution works? Sure, but that would be wasting a valuable learning opportunity.

I tried pythontutor but it does nothing to help me understand what’s going on. It shows me that the code from the solution:

def reversed_list(lst1, lst2):
  for index in range(len(lst1)):
    if lst1[index] != lst2[len(lst2) - 1 - index]:
      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]))

Creates two lists from the first print statement, then creates index, assign 0 to index, then 1, then 2, then return True.
I know that’s what going on because I know that the code has to iterate through all the elements and return True in the case of first example but still it does nothing to explain how? I know some other things happen other than changing values of index but WHAT are those other things?

It’s not that I’m lazy but I just spent last hour trying to understand what’s going on, trying to put it on paper. I understand the syntax but I just have no idea how all of this works as a whole. I feel like expalanations we were given so far are not enough for me to get it, like they taught my all about words but not how they work in a language if that has any sense.

Please, I need at least a hint : (

hm… yea, the steps where not as clear as i hoped

okay, lets add this:

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

okay, so i print three things:

  • the value of lst1 at index
  • the index used for lst2
  • the value of this index

make sure you understand what i print here, now run the code again. Does this tell you anything?

The important thing here is that you gain the insight in what is happening. That is better then any explanation.

one more hint (please do the above first):

the exercise solution compares the first value of list 1 with the last value of list 2, if these values are the same it will compare the second value of list 1 with the second to last value of list 2 and so forth

1 Like