Could I solve this using reverse()? Is it efficient?

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)!

def reversed_list(lst1,lst2):
if list(reversed(lst1)) == lst2:
return True
return False


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.

I used:
def reversed_list(lst1,lst2):
–if lst1 == list(reversed(lst2)):
----return True
----return False
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.


I’m confused why this should have returned “False” since they are in fact identical lists when put in reverse order? Both evaluate to [5, 3, 1].

Don’t mind me, I haven’t woken up yet. Sigh haha.

Again I didn’t use a loop :zipper_mouth_face:

def reversed_list(lst1, lst2):
  return lst1 == lst2

@venator86, In general, if there is a built-in function that accomplishes something you’ve been assigned, then using that function probably does not satisfy the assignment.

The exercise, as with max(), min(), len(), and so many others, including, later, sort(), is to teach you to think algorithmicly. Exploring Python’s built-ins and available modules is always to be commended, but is not always what your instructor has in mind for a particular exercise.

Did not your third-grade teacher insist that you learn long division and multiplication before allowing the use of calculators?

1 Like

I appreciate your stance, but my personal learning path is simply mastery of the tools, and solving problems with those tools. I would counter and say if a loop isn’t necessary to accomplish the task, then it shouldn’t be used. An early mentor advocated not being a “hammer mechanic” which I’ve tended to stick to, meaning when you fix a car, you don’t just hit everything with a hammer, you use the appropriate tool to arrive at the appropriate outcome.

I do solve it with a loop, then I traditionally see if I can solve it a second or third time using less code. I learned to post with the spoiler recently, so I try to blur any solution I post.


Trying hard here not to respond with a metaphor addressing the best way to learn to become a master mechanic. :slightly_smiling_face:

I’ll just point out that your “solution” does feature a nice example of returning a boolean comparison…

1 Like

Haha like I said, I do solve it with a loop first, I just try to iterate and optimize once or twice before I move on as well. Don’t worry, no step skipping over here :slight_smile:

1 Like

Another issue is that lst2 is mutated, which may not be desirable if we wish to keep both lists intact and unchanged.

return lst1 == lst2[:].reverse()

Above we mutate a shallow copy, not the original.

Would need to create the copy first. Then reverse it. Then compare. .reverse() returns ‘None’, so all comparisons evaluate to False.

1 Like

Good catch.

    temp = lst2[:]
    return lst1 == temp
1 Like

So the confusion comes from the way CodeCademy decided to word the problem. It specifically states multiple times “reversed list” which is NOT what they are actually looking for us to do. Iterating backwards through a list is NOT the same programmatically as reversing a list.

Reversing lists was covered in the material however iterating backwards was not reviewed at all.