# 12. Reversed lists checker(practice problem)

Hi Sir/ Ma’am, i am not understanding what i did wrong in my code. When i am submitting the answer the is_reversed variable is coming red. Also the answer given is not understood. Help will be appreciated in both, thanks. Struggling.

My Code:

``````for i in range(len(values1)):
if values1[i] == values2[-i-1]:
is_reversed = True
else:
is_reversed = False

print(is_reversed)
``````

What actually happened:

``````
``````

You should stop iteration once you find that one of the element is not matched.

``````else:
is_reversed = False
break
``````

Without break it will eventually end up with result of last elements comparison.
For example

``````values1 = [1,2,3]
values2 = [3,4,1]
``````

As per your logic we will iterate over `value1` and `value2` from backward list. So

First iteration we will check first element of `value1` and first from last element of `value2`
is `1 == 1` yes then we will set it flag `is_reversed=True`

Second iteration we will check second element of `value1` and second last of `value2`
is `2 == 4` yes then we will set it flag `is_reversed=False`

Now we know it is not matching so we can say list is not reversed. and we can stop iteration further from here using `break`

but if we don’t stop here it will go for next element third iteration
Third iteration we will check third element of `value1` and third from last element of `value2`
is `3 == 3` yes then we will set it flag `is_reversed=True`

So here it changed from `False` to `True` so as i said we will eventually end up with result of last elements comparison.

4 Likes

it worked. Can you explain more i didnt understood. Thanks for quick reply

Hi @uditchauhan,

I tried your code on the mentioned screen, as below:

Except that the logic needs to be reworked because on every comparison, the is_reversed variable is getting overwritten. The final value of is_reversed would correspond to the last elements in each list. So, you can either print the is_reversed variable at each loop iteration or keep a count of the number of mismatches.

Good luck

1 Like

@niranjana.r.iisc Thank you for your response

I have updated my answer and add some explanation. Hope it will help. I would suggest you to do debugging of your code on paper.

1 Like

i will try to understand your explanation

1 Like

It helped. Thanks a ton Also in the first solution given by dataquest they first printed is_reversed = true and then said that if the value is not equal then false. Did i got that right? We can do as per my code too right?

Yes.

Yes that is also right.

1 Like

okay thanks @DishinGoyani

I did something I considered easier and shorter. I tested in the practice program an it worked. This is my program to check if one list is the reversed of another one:

values1 = [80, 109, 111, 109, 94, 93, 108, 107, 81, 111, 101, 114, 102, 81, 107, 120, 108, 92, 113, 119, 97]
values2 = [97, 119, 113, 92, 108, 120, 107, 81, 102, 114, 110, 111, 81, 107, 108, 93, 94, 109, 111, 109, 80]

is_reversed = values1 == list(reversed(values2))
if is_reversed is True:
print(is_reversed)

1 Like

I just wanted to chime in on an alternative solution for any more newcomers like myself. Here is something I learned from @monorienaghogho previously on reversing lists. You can find more about `values1[::-1]` here Reversing a list(practice problem) - #4 by monorienaghogho

``````values1_reversed = values1[::-1]
if values2 == values1_reversed:
is_reversed = True
else:
is_reversed = False
``````

By using `values1[::-1]"` we created a list that is the reverse of the original `values1` list. After that, it is pretty straight forward and I think quite readable! I find the original solution to be far more prone to error than this personally. I’ll still probably learn it though for practice.

If my solution could be better, please let me know! I’m pretty new here.
Cheers!

2 Likes

I can see an alternative to your code. A one- liner:

``````is_reversed = values2 == values1[::-1]
``````

It’s not an alternative in the sense that it’s better code, just a different way of writing it.

Though, I haven’t tested it. I only have my phone at the moment.

2 Likes

I can confirm `is_reversed = values2 == values1[::-1]` works with only 1 line!
That is satisfying. Thank you!

2 Likes

No worries @Matt.Kriese.

Happy learning.