3 posts were merged into an existing topic: How can I iterate over a list backward?
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 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?