FAQ: Code Challenge: Loops - Reversed List

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

2 posts were merged into an existing topic: Sharing solutions

2 posts were split to a new topic: What do you think of this solution?

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?