 CODE CHALLENGE: LOOPS Reversed List. " - 1 - "

In this challenge the hint gives theses instructions:

" Let’s say the list are of size 5. You want to compare lst1 with lst2 , lst1 with lst2 and so on.

Loop through the numbers created by range(len(lst1)) using a variable named index

Compare lst1[index] to lst2[len(lst2) - 1 - index] . If those two items are not equal, return False . If you loop through the entire list and you never return False , that means that every item was equal, and you should return True ."

Can someone explain what the " - 1 - " from “lst2[len(lst2) - 1 - index]” part of the hint means? I don’t remember this showing up before.

indexes are zero based (counting start at zero), while len() starts counting at 1. so you need to subtract one to compensate for this difference.

3 Likes

I understand subtracting 1 from the index, but I have never seen the " - 1 -" operand used before. Maybe I’m not explaining myself correctly but is this style of subtracting from the index commonly used? Also, if there is a link someone could provide how to use this style of subtracting from the index to explain it, would be much appreciated. I’ve tried looking.

- 1 - in this instance isn’t a single operator, it’s two individual subtraction operations.

len(lst2)
- 1
- index 3 Likes

thepitycoder is right, in math we can perfectly subtract twice: 6 - 3 - 2 = 1

in programming we can do this as well.

4 Likes

Thank you that’s exactly what I was asking. Thank you!

1 Like