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

how about this one? :slight_smile:

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