Could someone describe what happened in alternative solution to the task?

link to mission: https://app.dataquest.io/m/1010/conditional-statements-practice-problems/12/reversed-lists

Screen Link:

values1 = [80, 109, 111, 109, 94, 93, 108, 107, 81, 111, 101, 114, 102, 81, 107, 120, 108, 92, 113, 119, 97]
values2 = [97, 119, 113, 92, 108, 120, 107, 81, 102, 114, 110, 111, 81, 107, 108, 93, 94, 109, 111, 109, 80]
is_reversed = True
for i in range(len(values1)):
    if values1[i] != values2[-i - 1]:
        is_reversed = False
        
# Alternative solution:
is_reverse = True
for i in range(len(values1)):
    if values1[i] != values2[len(values1) - i - 1]:
        is_reverse = False

I don’t understand this alternative solution. How does it works ? what would this code do behind the curtain?

to be more specific: I don’t understand this part:

values2[len(values1) - i - 1]
1 Like

Hi please provide a screen link as per these guidelines so that we can better assist you.

1 Like

Hello @drill_n_bass, to understand this let’s see this example with a list with fewer values.

values1 = [1, 2, 3, 4]
values2 = [4, 3, 2, 1]

Both lists have the same length, and also values1 is values2 reversed.
in values1 let’s start with the index in the first value, it is index 0, the responding index in values2 will be index 3 to get this 3 we did 4 - 0 - 1 that gives 3, and thas why they used values2[len(values1) - i - 1]

Let’s Understand the item in index 2 in values1, the corresponding item in values2 is in index 1, to get that 1 we did 4 - 2 - 1.


I hope you’ve understood now.

thank you :slight_smile:
…
I think I found another way/solution

for i in range(len(values1)):
    if values1[i] == values2[-i : -1]:
        is_reversed = True
print('is_reversed =', is_reversed)

just wonder: is it works in a such way that I should put above:

is_reversed = False

It would be - also - opposite to solutions listed above

1 Like