# How can I iterate over a list backward?

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):
...
``````

I have not learned this form of slice

If you know it’s a slice then you arguably know enough. The arguments are like that of range, the step there is -1 and start/stop get default values when they are left out. So it just says: step backwards.

The main point though, is that complicated math with indexes are something to be avoided if possible. We can abstract that away, remove the possibility of getting it wrong.

I didn t understand why
lst2[len(lst2) - 1 - index] would reflect the reverse of the indexes, in base of which logic reason?

1 Like

len(lst2) - 1 will give you the right most value, by increasing `index` variable you will go from right to left over the list

Still don’t get this…

``````lst1[index] != lst2[len(lst2) - 1 - index]:

# I struggle to understand what it means:
lst1[index]  - # starts at 0 ?
lst2[len(lst2) - 1 - index #I read as starts at 2 - 1 - index
``````

Write down some word on a piece of paper in big letters.
Point your left hand at the first letter
Point your right hand at the last letter
Repeat:
Compare what you’re pointing at
Move both hands towards the other end of the word

1 Like

tnx; did that and run it trough the vscode debugger.

it’s something like this:

3 - 1 - 0 = position : 2 = <number 1>
3 - 1 - 1 = position : 1 = <number 2>
3 - 1 - 2 = position : 0 = <number 3>

makes sense now…

still don’t know why I can’t use reverse()

Thanks a lot, It’s much more clear now!

I remember it was covered but failed to remember…

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

I am really confused as well. I don’t know why I have to unindent “return True” instead of writing:

else:
return True

Hello, @ulwianamehta-malhotr. Welcome to the forum.

`return` stops the execution of your function, and returns the value included in the `return` statement (if one exists) to the caller (the line of code that called the function). If you want your `for` loop to make more than one iteration, you can’t can’t `return` anything until it is finished. See this post for an additional explanation: FAQ: Code Challenge: Loops - Reversed List
Happy coding!

Also, please use the `</>` button in the menu bar above where you type to format your code. This is especially important with Python where indentation is not optional. Click the `</>` button first on a blank line, and then paste your code in the space indicated.
Thanks!

Hi! I figured it out! The unindent actually takes the place of an else statement. It means the same thing!

Sorry, can you explain the rules of this game please? I could not find it by the name…Is it correct that we can’t see the numbers until we “flip them from the right hand side”? Or we just need to find the correct place for the end number in the random list?

can someone clear me how can we create a new list (that contains values and that contains indices of the values) from comparing 2 lists ?
for example if we have 2 lists [5,9,23,12] and [1,9,3,12] how can we create a list of same values i.e [9,12] (values) and list of indices of same values i.e [1,3] (indices)? Please
Thanks

It’s been decades since I first heard of this game.

Think of a scrambled list of numbers, starting with lowest difficulty, three digits.

``````3 1 2
``````

Flipping only from the right, we would select the 1 and flip the pair, which will give

``````3 2 1
``````

Now flip all three and we have the complete order,

``````1 2 3
``````

Next we got up one digit,

``````2 4 3 1
``````

The first flip will be all four,

``````1 3 4 2
``````

Next flip the right three,

``````1 2 4 3
``````

Then the final flip, the last two digits,

``````1 2 3 4
``````

Now five digits, which illustrates multiple possible strategies.

``````4 1 5 2 3

4 1 5 3 2

4 1 2 3 5

5 3 2 1 4

5 4 1 2 3

5 4 3 2 1

1 2 3 4 5
``````

That took five flips. Can we do it in less?

``````4 1 5 2 3

4 1 3 2 5

5 2 3 1 4

5 4 1 3 2

5 4 1 2 3

5 4 3 2 1

1 2 3 4 5
``````

Also five flips.

The game continues up to nine digits. A score less than,

``````(3 + 9) / 2 * 7 => 6 * 7 => 42
``````

is considered below par. Like in golf, the lower the score the better.

1 Like

Since we want (I’m assuming) only matches at the same index we can create a zip object of paired values.

``````>>> a, b = [5, 9, 23, 12], [1, 9, 3, 12]
>>> c = zip(a, b)
>>> d = []
>>> for m, n in c:
if m == n:
d.append(m)

>>> d
[9, 12]
>>>
``````

If you are not familiar with the `zip` constructor, it essentially merges two or more lists into a list of tuples.

``````>>> c
[(5, 1), (9, 9), (23, 3), (12, 12)]
>>>
``````

When we iterate a zip object all we need is enough variables to be able to unpack each tuple, in this case two,

``````for m, n in c:
``````

This may be new to you, too. Consider,

``````x = (3, 5, 7, 9)

a, b, c, d = x

print a    # 3
print b    # 5
print c    # 7
print d    # 9
``````

list objects can be unpacked in the exact same way. Of course the shorter the sequence the more it makes sense to unpack, where a longer sequence is better iterated than unpacked so there are not a lot of variables in play.

At some point you will learn about comprehensions, if not already learned. We can write a single line solution to the problem which returns a list…

``````>>> a, b = [5, 9, 23, 12], [1, 9, 3, 12]
>>> [m for m, n in zip(a, b) if m == n]
[9, 12]
>>>
``````

It follows that we could write this as a reusable function…

``````def matched_pairs(a, b):
return [m for m, n in zip(a, b) if m == n]

print (matched_pairs([5, 9, 23, 12], [1, 9, 3, 12]))
# [9, 12]
print (matched_pairs([12, 16, 20, 24, 28], [4, 5, 20, 21, 28]))
# [20, 28]
``````

!!! Thank you!! It’s so interesting!

Sorry, seems too much information for my poor head for the moment, but I can’t stand: what formula did you use? Why?