FAQ: Code Challenge: Loops - Reversed List

Hmm, but I needed it to match the next interation against the correct index. How else can I cycle through index on lst2?

Wow, i need my coffee.

You could also loop over the indexes of lst1:

for n1 in range(len(lst1)):

then use negative indexes (len(lst2) - 1 - n1) to get the items from list 2

But your solution also works.

1 Like
if lst1[n1] != lst2[index]:

Instead of tracking two indices, track the one, and make the other relative to it.

Note that we are assuming both lists are the same length.

    n = len(lst1)    # to avoid repetition
    if len(lst2) != n: return False

    for n1 in range(n-1,-1,-1):
        if lst1[n1] != lst2[n - n1 - 1]: return False
    return True

The above upholds all your design concepts without using index or having an else clause.