Why is this wrong?
when flipping True and False and changing == for != it is correct… I don’t understand why can’t it be done the other way around.
because return is the last thing a function does, when a return keyword is reached, the function ends
so with the code you currently have, if the first element of lst1 equals the last element of lst2, true will be returned, thus the function ends. At this point, we haven’t even verified all the other elements in the lists, so we can already have a false positive.
lets say the first element of lst1 doesn’t equal the last element of lst2 (at which point false should be returned, which isn’t happening)
the loop will continue with the second and element second to last element of lst2, compare them, if they equal, return true. Yet again false positive
def reversed_list(lst1, lst2): a = 1 for i in range(1, len(lst1)+1): if lst1[i-1] != lst2[-i]: a *= 0 break return bool(a)
I’ve defined the range to look more familiar - like 1-1 = vs [-1], 2-1 = vs [-2] etc. The function returns variable a which is equal to 1. In case the values in a pair don’t match each other, variable a turns into 0. I’ve put break here because the only inequalty in pairs is enough to conclude that lists don’t match the task, so we don’t need to run the loop further on. The function returns boolean value of variable a.
Found my code to be a different solution, so I thought I could share:
def reversed_list(lst1, lst2): while len(lst1) > 0: if lst1 == lst2[-1]: lst1.pop(0) lst2.pop(-1) else: return False return True
No need to create a third list.
I saw your solution and I’d like to share another way:
def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2)-1-index]:
suma = 0
for j in range(len(new)):
suma += new[j]
if suma == len(new):
Thanks for share your knowledge!
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?
What did the hint look like? Maybe we can make sense of it.
@nope4633285348, this approach has advantage that you don’t have to reverse the whole list first.
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.
I think I got it, thank you!
Not sure if I’ve done this the “right” way that codecademy wanted, but this seemed to make the most sense to me (and is efficient)!
if list(reversed(lst1)) == lst2:
What is your definition of efficient here?
Hmm, that it doesn’t need many lines of code?
Perhaps there is a nuance for this exercise that I’m missing?
but is that the only factor defining efficiency?
What if you lists contain 10,000 to 100,000 items? You first need to reverse that entire list, and then loop it again to do comparison.
maybe, but not something to worry about at this point in your learning process.
–if lst1 == list(reversed(lst2)):
which works wonders for me
Yes, but like i said here:
have you thought about that?
It compares every value (which is what you want for it to work) to the reversed list. If it finds a value that does not meet the condition then it will terminate and return False. So it’s only really a problem if one of the last values it encounters doesn’t match.
but is that truly efficient? Lets say you have 100,000 items in your list, you first need to reverse 100,000 items, and then do 100,000 comparisons. Surely, there must be a more effective way?
But then again, performance isn’t the only benchmark.
Why does the code below not work?
def reversed_list(lst1, lst2): lst3 = lst2[::-1] for index in range(len(lst1)): if lst1[index] == lst3[index]: return True else: return False
It returns True for both tests!
return means handing back data, which means the function is done. However, in your program, this means the loop only makes a single iteration.
so the only compared are the first value of lst1 and the last value of lst2. If these values are equal, your program assumes lst2 is the reverse of lst1.