What does `lst2[len(lst2) - 1 - index]` do?

Is there a reason we can’t simply use:

return lst1[::-1]) == lst2

To compare if a list and another list backwards are the same? I understand the idea behind the negative index and -1, but I’m not seeing why it’s necessary and feel I might be missing something.

List slicing is a simple and effective approach, though we should be confident in our ability to solve this without using slices.

This line is simpler and works the same:

if lst1[i] != lst2[-(i+1)]:

Is there a need to add -0? Won’t it be 4 anyways?

i just wrote out one of the iterations, that 0 will change depending on which loop iteration the loop is on

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

of this code above, I could not grasp logic because I don’t actually understand the right side of the equation in the first place.If someone could explain?

Did you read the replies in this topic? Surely there is an explanation in here

apart from the code, what is this condition suppose to do? It compares each value with its counter part (comparing first item of lst1 with the last item of lst2)

so what does the right hand side do? Getting the last value of lst2 in the first iteration of the loop, then the second to last item and so forth.

So to be more specific under normal circumstances “len” prints the number of elements in the list right?Is that for the same reason used here?
Also what does " minus 1" and " minus index" do here?
Is “minus 1” used to reduce the number of elements (len) for each loop iteration?
I have no idea about “minus index” do in the end.(I of course know what index, I just dont know the relation of these codes within eachother)

if we know the amount of elements in the list, we also know the last index of the last element in the list (length minus one), given length starts counting at one while lists are zero indexed based

what about index? Maybe you should print(index) to get some more information, to see what it does. You can also use tool like this to step through your code, and inspect its state at every step.

Great explanation! Thanks!!!

Even though your code has more steps…i liked the way it reverses the second list2 and then compares both lst1 & lst2!! Clever code i think! And a different perspective!

Great explanation, thanks! :smiley: