Reversed List

I got it right but I don’t get it, could use some help. I have questions.

def reversed_list(lst1, lst2):
  for index in range(len(lst1)):
    if lst1[index] != lst2[len(lst2) - 1 - index]:
      return False
  return True
  
  
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))

You might be better served by solving this yourself in a way you know how (even if it’s less efficient or takes more lines). Guessing how things work is a fast-track to getting bugs in your code.

As for questions; what questions do you have? Boil it down to a single expression or very simple code (a minimal working example) and detail what you’ve tried and what did and did not work as you expected.

3 Likes

The only thing I don’t understand is the use of true and false in this challenge, the indentation as well

If you’re determined to work backwards then you’ll want to break this function down line by line.

What values would for index in range(len(lst1)) be providing?

The if statement has a lot going on but separate it out first. Work out what values your indices would take.
For an example consider what happens when index is 0.
lst1[0] != lst2[len(lst2) - 1 - 0]
What is lst[0]?
What index is lst2[len(lst2) - 1] addressing?

Consider this for repeating indices. Since there are only three elements to the example lists you could work line by line with pen and paper if you’re struggling with the control flow.

If we compare lst[0] and lst2[len(lst2) - 1] what are we comparing?

1 2 3

lst2[3-1]

lst[1} and lst[1]

Double check all your assumed values here, that range isn’t quite right, nor is the final comparison.

It was closer on the previous line, lst2[3-1].

So when that line is run with index=0 the expression is lst[0] != lst2[3-1]. Specifically which parts of these two lists are being addressed?

lst[0] = lst[1], 0 is the index 1 is the element
lst[3-1] = lst[2] = lst[1], 2 is the index 1 is the element

So lst[0] != lst[3-1], is lst[1] != lst[1] ?

There are two different lists here, lst and lst2. If the names are causing an issue consider changing them to something more readable.

You seem to be confusing the way indexing works for lists. I highly suggests going back over some of your previous lessons to make sure you understand this. It’s not something you can gloss over, it’s essential syntax to learn. Take the time to wrap your head around it.

For a quick example that doesn’t store integers to make things more confused…
lst[0] addresses the first elment of list lst
lst[1] would address the second element of lst

lst = ['a', 'b'. 'c']
lst[0] == 'a'
lst[1] == 'b'

lst1 is [1, 2, 3]
and
lst2 is [3, 2, 1]

lst1[0] != lst2[3-1-0] is that right

That would be a partially simplified expression when index = 0. What elements of your two lists are you addressing in this case?

So for lst1, it would be 1 and for lst2 when simplified [3-1-0] = [2] it would be 1