Reversed List exercise

Hi all.
I’ve tried doing the excersice the way I understand, but doesn’t seem to work, and I can’t see why. Can anyone explain what am I doing wrong?
The way I’m trying to do is creating a list starting from the last element of the list and then removing that last element, and then looping through it. In theory it should work, but doesn’t!

def reversed_list(lst1, lst2):

  reversed_lst2 = []

  for i in lst2:

   reversed_lst2.append(lst2[-1])

   lst2.remove(lst2[-1])

  if lst1 == reversed_lst2:

    return True

  else:

    return False

link https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-loops

You are modifying lst2 while looping over it.
This will create all sorts of problems with the indexing.

Step 2: Loop through every index in one of the lists from beginning to end
So use for i in range(start, end, step)

2 Likes

You should also be aware that lst2.remove(lst2[-1]) would be modifying the list that was submitted. If you are comparing 2 data sets in your program it’s unlikely you would want to consume them in the process.

It’s important to be familiar with for index in range(len(list)) loops, but if you’re interested in how to do it without you could use the insert method. Python List insert() Method For every item in the list, you insert it at the beginning. It probably isn’t as resource friendly as the indexing method though.