# How can I iterate over a list backward?

Hello. I don’t understand why this code isn’t working:

``````def reversed_list(lst1, lst2):
for i in range(len(lst2)):
lst2[i] = lst2[-i-1]
if lst2 == lst1:
return True
else:
return False
``````

Why is this returning False?

``````print(reversed_list([1, 2, 3], [3, 2, 1]))
``````

the reversing of your list doesn’t go well. manipulating the same list as you are looping over is very tricky

if you print `lst2`, you will see: `[1, 2, 1]`:

``````#Write your function here
def reversed_list(lst1, lst2):
for i in range(len(lst2)):
print(i, lst2[-i-1])

lst2[i] = lst2[-i-1]
print(lst2)
if lst2 == lst1:
return True
else:
return False

#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
``````
1 Like

Ok, I’ll keep that in mind. Thanks.

I found this to be the easiest way.
Decided to check if anyone else had the same idea but I didn’t see it here so I figured I’d share.

``````def reversed_list(lst1, lst2):
if lst1 == lst2[::-1]:
return True
else:
return False
``````

given a comparison gives a boolean value as result, we can just return that:

``````def reversed_list(lst1, lst2):
return lst1 == lst2[::-1]
``````
1 Like

This is what I did…

the other way i.e. “the hint”, while I can see what it does…sort of…confuses me.

I completed this using very little code and it seemed to do the trick. Everything else I’ve seen here seems to over complicate this problem, even the hint of the solution

``````def reversed_list(lst1, lst2):
lst2.reverse()
if lst1 == lst2:
return True
else:
return False
``````

are you familiar with the big O notation? Its interesting, i recommend to do some reading about it, then think what the big O notation for the exercise solution is and what the big O notation is for your solution.

Simplicity and complexity (and many other factors) each have there pros and cons, as a programmer its your job to known the pros and cons, consider them, then choice the right implementation for the job.

Mmmh they both have linear upper bounds.

1 Like

``````def reversed_list(lst1, lst2):
for i in lst1:
if lst1[i]==lst2[-i-1]:
return True
else: return "False"
``````

You could test it.

``````def reversed_list(lst1, lst2):
for i in lst1:
if lst1[i]==lst2[-i-1]:
return True
else: return "False"

a = [1,3,1]
b = [2,3,1]

if reversed_list(a, b):
print(f"{a} is the reverse of {b}")
else:
print(f"{a} is NOT the reverse of {b}")
``````

my code works, but it returns this:

It tells you what input it supposedly failed for, so you tried that input. Right?

yes. the loop works fine. the answers are as expected i.e. True, False. but it still shows the error

but what was the input?

You could make a function like this:

``````def f(whatever, man):
return True
``````

and it’d be right about half the time. I can make LOTS of tests where this is correct. But that doesn’t mean it is.

You have an error message telling you about input that your function doesn’t handle. so test it with that input?

I still don’t know how to fix it

if we don’t want to modify original list (and as alternative to list slicing) we also can create new list from reversed iterator of original list with `list(reversed(original_list))`.

I have seen the other solutions to this problem, but was curious if I could shorten my own in some way. Is there a way to shorten my ‘check’ or lst? I saw some solutions put return False inside the if statement and then return True outside the loop, but I fail to see how this would be different than the for loop returning True in the first loop if the True was inside rather then outside, which I attempted before my workaround list.

``````def reversed_list(lst1, lst2):
lst = []
for i in range(1, len(lst1)):
if lst1[i - 1] == lst2[i * -1]:
lst.append(1)
else:
lst.append(0)
if lst.count(0) == 0:
return True
else:
return False
``````

that is a massive difference. Doing:

``````if lst1[i - 1] == lst2[i * -1]:
return True
``````

now if the first element of lst1 equals the last element of lst2:

``````reversed_list([1, 3, 5], [2, 4, 1])
``````

we get True. The if condition is True in the first iteration of the loop, so a return keyword is reached, the function hands back data and is done.

Hi. I have solved the question with the I[::-1] method, but after reading the comments decided not to use slicing and came up with the following solution:

Do you think it is good enough? Or should I try to implement a solution that will actually make a proper algorithm? Don’t want to be cheating