How can I iterate over a list backward?

Can some one please explain why this code does not work? Solution use a =! condition I have used == condition

  for x in range(len(lst1)):
    if lst1[x] == lst2[len(lst2)-1-x]:
      return True
  return False
1 Like

Because it returns True without going through the whole of either list.

Not finding a match would give cause to return False, and exit the function.

To return True, we must have matched every character of both strings. The loop needs to complete before we can draw that conclusion.

2 Likes

Hello,

can someone please explain me 2 things?

a)Why is the second number in range for going backward -1, and not (+)1?

I thought the second number in a range of 3 numbers can’t be lesser than 1, since in that case it would represent an element with index 0, no? If the second number is -1 doesn’t it represent an element with index -2?

b)What are the 2 colons in the example given for going backward using list slicing?

for i in list [ : : -1 ]

Thank you.

First off, did you write an algorithm to reverse the list without slicing? It’s important that you learn a couple of ways to do this before jumping on the wagon of Python built-in methods.

As far as the slice goes,

[ start index : end index : stride length ]

A negative stride length indicates moving from right to left in the list.

https://stackoverflow.com/questions/44385999/how-to-explain-the-reverse-of-a-sequence-by-slice-notation-a-1

1 Like

Thank you sir, I see there’s a lot explained in the link you provided, I definitely have to take some time to go through it.

1 Like

for this specific lesson, why wouldn’t the following solution work? it’s saying it’s not the correct solution, but it does what it is asking, right? am i missing something?

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

Slicing is a viable solution, but may not be what the SCT is expecting. Perhaps create an algorithm that compares iteratively rather than using the slice method.

1 Like

Is there an inbuilt function to reverse a list?

Yes there is,

 list.reverse()

as well as the slice,

 list[::-1]

In the learning stages we will often focus on naive, iterative approaches.

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

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

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
1 Like

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.

1 Like

This one worked for me:

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