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

I honestly don’t have a clue why this line works, specifically the - index part. I understand the rest of the code just fine when I read it out loud…

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

What does - index do exactly? All I know so far is that we take any index from lst1 and if not equal to the last index of lst2 - index(???).

1 Like

lets say we have the following two lists:

lst1 = ['a', 'b', 'c', 'd', 'e']
lst2 = ['e', 'd', 'c', 'b', 'a']

lst1 is the same as lst2 in reverse. Now we have to express this into code

what we need to do:

  • compare first item of lst1 with last item of lst2
  • compare second item of lst1 with second to last item of lst2
  • compare last item item of lst1 with first item of lst2

(... represents everything in between, i was too lazy to write them all)

this exactly what this if condition does, for the first iteration of the loop, index will be zero, so then we get:

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

lets do the math first, lst2 has a length of 5 (sticking with the example i started this post with). 5 - 1 - 0 is 4, so we get:

if lst1[0] != lst2[4]:

now we resolve the square brackets, which gives us the values from the lists based on indexes:

if 'a' != 'a':

these are equal, so the lst1 could still be the same as lst2 in reverse, so we continue to the next iteration of the loop, index has now become 1:

if lst1[1] != lst2[len(lst2) - 1 - 1]:
# resolving math
if lst1[1] != lst2[3]:
# getting values/resolving square brackets
if 'b' != 'b':

these are equal, so the lst1 could still be the same as lst2 in reverse. Here we compared the second value of lst1 with the second to last value of lst2.

This continues until we completed all the elements from the list, or one of the values doesn’t equal, in which case lst1 isn’t the same as lst2 in reverse, in which case we should return False

44 Likes

Ohh now I get it, thank you for your through explanation! Some of these concepts really need some time to sink in.

8 Likes

stetim94, I’m struggling a lot at the moment to understand this line of code, particularly the “-1 -0” bit and how it’s operating to locate the index it needs to compare. :confounded::woman_facepalming:t4:
Can you or anyone elaborate a little and “explain it to me like i’m 5”???

2 Likes

Carry it out manually and observe what it has you do?
Or approach it from the other direction, think about what you want to happen and what actions are involved in that.

How does the index of the last value relate to the length of the list?

To visit a location one step closer to the start, what difference would you need to make? For two steps, you would apply this difference two times, and so on.

Thank-you for replying @ionatan
The -1 I was understanding to be how we access the end of a list when we don’t know the length of it. Is that right? So as we move further down the indices of lst2 that negative integer would increase. Assuming that is correct, then why do we include the index in the brackets? Doesn’t [len(lst2) - 1] provide the appropriate index we are comparing? I’m missing the whole reason that zero (index) is included.

2 Likes

You can’t have a list without knowing its length since that information exists in the list.

If you’re to look up many locations and you’re using the same expression to describe those locations, then something in that expression has to change, some part of it has to be variable, or you would be looking up the same location each time.

Python lists do define meaning to negative indexes if that’s what you’re on about, but that’s equivalent to positive ones since they can both address the same locations. So you’d pick one, and then consider how to compute the index you’re currently looking for, the strategy for that will usually be to consider what the first location is, and what changes during each step so that you can apply this change each time. When iterating forwards you’d start at 0 and the difference between iterations is +1, for going backwards, you’d figure out the last index, and the difference would now instead be -1

If that was the whole expression then the result of that expression would be -1 which would refer to the last location. However, that is not the full expression so you would have to evaluate the whole thing.

def reversed_list(lst1, lst2):
  rev_lst = []
  while len(lst2) > 0:
    a = lst2.pop(-1)
    rev_lst.append(a)
  if lst1 == rev_lst:
    return True
  else:
    return False

The only way I managed to solve this. Still quite confused with the hint, tbh. But this seems to work fine?

1 Like

What did the hint look like? Maybe we can make sense of it.

1 Like

image

@nope4633285348, this approach has advantage that you don’t have to reverse the whole list first.

2 Likes

Yeah, that was going to be my suggestion, and I see the hint is actually spot on.

We can write the comparison like,

lst1[i] != lst2[-i-1]

with the same effect.

4 Likes

I think I got it, thank you!

Really helpful, line by line, concept by concept breakdown.
Thanks!

Yes, and the trick is next time you can do this step by step breakdown yourself :wink:

1 Like

Hi guys,

I understood the solution, but seems to me a bit over-complicated and couldn’t understand why mine returns the wrong, opposite result. Could someone explain it to me?

This is what I tried:

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

your code compares the first item of lst1 with the last item of lst2, based on this comparison, either True or False is returned.

Which is no true way of testing of the lists are reversed of each other.

Oh sure, I see. Thanks!

we could use this :

lst2[-(index+1)}

You seem to know what your doing. I have no sense on what’s going on. How do you improve in thinking logically about the solutions.

By first falling on my face, and trying not to do that again. Study what the code can do. What is allowed? Why do I need a particular behavior? Eventually, we begin to see code as a tool. We need an 11/16 socket to get that spark plug out. Code is just the same kind of tool for a different kind of engine.