Sharing solutions

A simple solution…

def reversed_list(lst1, lst2):
    return lst1 == lst2[::-1]
#Write your function here
def reversed_list(lst1, lst2):
  is_reverse = True
  for i in range(len(lst1)):
      if(lst1[i] != lst2[-1-i] ):
        is_reverse = False
  return is_reverse
#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))

def reversed_list(lst1, lst2):

lst2.reverse()

if lst1 == lst2:

return True

elif lst1 != lst2:

return False

Not really in the spirit of learning loops but this also works.

Hello @method8360736245 and welcome to the Codecademy Forums!

Looking at your code right now, it would only work if the elements of lst1 are in order from least to greatest. For example, the following code returns False instead of the expected True.

reversed_list([1, 3, 2], [2, 3, 1]) # returns False

What changes could you make to ensure your function works for any list inputs?

Hint: This lesson focuses on applying your newfound knowledge of loops. Try using loops and iteration in your solution.

def reversed_list(lst1, lst2):
  if len(lst1) == 0:
    return True
  rev1_list = []
  index = range(0, len(lst1))
  for i in index:
    rev1_list.append(lst1[-(i+1)])
  for i in index:
    if rev1_list == lst2:
      return True
    return False

And after inspiration from the above comments, I optimised down to this:

def reversed_list(lst1, lst2):
  for i in range(0, len(lst1)):
    if lst1[i] != lst2[-(i+1)]:
      return False
  return True 
1 Like

def reversed_list(lst1, lst2):
lst2reversed =
for element in range(len(lst2)):
lst2reversed.append(lst2.pop())
if lst2reversed == lst1:
return True
return False

this is the way i came up with

Hi!

This is the first time that I try to learn how to code (in any language), and seeing some of the replies in this forum, I see that the solution I came up with is too much complicated and could be easier… However, what I came here for is asking for help in understanding why my coding is not working…

I’ll share with you what I did in case anybody may help:

def reversed_list(lst1, lst2):
reversed_lst2 =
remaining_lst2 = lst2
for i in range(len(lst2)):
reversed_lst2.append(remaining_lst2[-1])
remaining_lst2 = remaining_lst2[:len(remaining_lst2)-1]
for j in range(len(lst2)):
number_of_equals = 0
if lst1[j] == reversed_lst2[j]:
number_of_equals += 1
print (str(lst1[j]) + " vs " + str(reversed_lst2[j]))
print (number_of_equals)
if number_of_equals == len(lst1):
return True
else:
return False

I have tried to debug it and, for some reason, the variable number_of_equals does not sum +1 on its previous number every time that it finds a match in the loop… It keeps being 1 with each iteration… Can anybody help me understand what’s wrong in my coding?

Thanks in advance for your help and regards,
Mario

Welcome to the forums! :slight_smile: For future posts, please format your code using the </> button.

Take a look at the code above. I believe your intention is to increment number_of_equals by one every time the elements at a certain index in both lists match. Remember that everything inside a for loop will be executed each time the loop iterates. This means that you are effectively setting number_of_equals = 0 every time the loop runs instead of retaining the existing number and incrementing it if necessary. How could you fix this?


Side note: yes, your solution could be more streamlined, but it’s great that you’ve solved the exercise this way. After using this naive approach, you know exactly, step-by-step, how this problem can be solved. It’s important to get comfortable with these new topics first before trying more complex implementations. Now that you’ve solved the problem this way, you can try to make your code more efficient and concise.

1 Like

Thank you very much!! I did not realize that and really really really appreciate your help! I just put the “number of equals” variable set to zero BEFORE starting the loop and it worked!!

1 Like