Codecademy Forums

How can I iterate over a list backward?

I don’t understand what this particular part of the code is doing:

lst2[len(lst2) - 1 - index]

Up until this point of the course, I haven’t seen this taught and how it translates to reversing the list. The solution to the code for the exercise is below, but I don’t get that part of it.

def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
return True
#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))

1 Like

It is accessing the second list with an index that is the same distance from the right end as index is from the left.

1 Like

Ok, so would:

lst2[len(lst2) - 1]

mean start from the right?

1 Like

Yes, that would be the last index from the left, and first index from the right, same as, lst2[-1].

1 Like

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

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

this returns #false false?

I know you need to do list(reversed(lst2)) but I can’t seem to get it to work here?

 lst2.reverse()
 return lst1 == lst2
2 Likes

Can you explain why it doesn’t work in the if statement?

1 Like

list.reverse() returns None. It just reverses the list!

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

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

# Output:
True
False
1 Like

Thank you, it seems to be a pattern that many methods .sort, .reverse, etc do not return anything, is there something to that?

They are in situ operations, as in the happen in place.

Just as list.sort() has its counterpart, sorted(my_list), list.reverse() has its counterpart, reversed(my_list). the latter in each case return a new list, where the former are in place.

2 Likes

This one worked for me:

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

Can someone tell me why this code(1) works:

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

And this code(2) doesn’t:

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

I don’t understand why code(1) doesn’t always return true regardless of lst1 and lst2 because I thought that the lower return value after the for loop would override the boolean of the function due to its indent and being the last call.

Any help with this would be much appreciated.

Many thanks.

Please preserve the indents by making use of the </> icon in the menu bar that appears atop the text box when you type in it.

So long as we can assume the given form,

for ...:
    if ...:
        return False
return True

then the logic is sound.

When there is a return True in the else clause within the loop then the function terminates at that point, without completing the loop.

@mtf Thank you for your very clear explanation. Much obliged :slight_smile:

1 Like

Hi guys,

I can’t figure out why the returns at the bottom of the code are indented like that. Any explanation so I can learn to use this format? See my posted picture/screenshot!

wow, this is super helpful! Learned a new way of writing code!

Why both continue and pass can complete this exercise.

#Write your function here
def reversed_list(lst1, lst2):
  for i in range(1,(len(lst1)+1)):
    if lst1[i-1] == lst2[-i]:
      continue
    else:
      return False
  return True

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


Doing nothing this iteration and moving on to next iteration is the same thing.
You only need to do something for one of the cases. You can remove the other.

if different:
    return False

Also, that code makes me nervous.
That math is really error prone, and it can be avoided.

Consider:

def is_reverse_of(a, b):
    return all(aa == bb for (aa, bb) in zip(a, reversed(b)))

or with fewer crazy things, I guess:

def is_reverse_of(a, b):
    a_is = range(len(a))
    b_is = range(len(b))[::-1]

    for ia, ib in zip(a_is, b_is):
        ...