Reversing a list(practice problem)

Hi can anyone give me clarity what does values[-i -1] is saying? Thank you.

Screen Link:
https://app.dataquest.io/m/1009/lists-and-for-loops-practice-problems/6/reversing-a-list

My Code:

reversed_values = []
for i in range(len(values)):
    reversed_values.append(values[-i - 1])
    
print(reversed_values)
1 Like

Hi @uditchauhan

You are given the list

values = [ 16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 17, 14, 1, 9]
# index:   0  1  2  3  4  5  6  7  8  9   10  11  12  13  14
#position: 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15

In the forward order, i starts from 0 and will count out to the length of the list, in this case the 15th position (that is the 0th up to the 14th index). - i - 1 starts instead from the from the back of the list -0 -1 = -1 (recall i is 0 at the start of the loop). Also recall that python is zero-indexed hence the index and positions are different from every individual element in the list.

Hope this helps!

9 Likes

thanks @masterryan.prof. So it will go on like -1,-2,-3… till the loop concludes right?

1 Like

In addition to what @masterryan.prof has said, kindly view the code below. It is the one of the quickest ways to reverse a list:

reversed_values = values[::-1] 
12 Likes

thank you @monorienaghogho for the tip. Does the syntax has a specific meaning [: : -1]?

2 Likes

[::1] means take every item on the list one after the other. Until the end.

[::-1] means take every item on the list starting from the ending. Remember that the last item on a list is indexed as -1. So it counts -1, -2, -3, ... until it gets to the first item.

6 Likes

Great , thanks a lot

1 Like

Yes @uditchauhan: just think of the logic as the loop continues. Note that what @monorienaghogho said does not work in this case because the loop will repeat and print the list i times, so you need to remove the loop for his method to work.

i.e.

values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 17, 14, 1, 9]

# Write your answer below
reversed_values = values[::-1]

print(reversed_values)

image

5 Likes

why this method you mentioned isn’t posted here? : https://docs.python.org/3/library/stdtypes.html#typesseq-range

I found your method on google too but, not in the documentation.

another solution (invented by myself):

reversed_values =
for n in reversed(range(len(values))):
reversed_val = values[n]
reversed_values.append(reversed_val)
[btw. why I don’t have indentation in my blockquote? how can I do it?]

I have one more question for all:

In reversed_values_dataquest_1 why 9 is missing ( why the last row/loop wasn’t printed)?

2 Likes

@drill_n_bass

The 9 doesn’t show because the last element you specify when you slice a list is not included in the result. You should use values[-i:] instead of values[-i:-1].

Cheers!


reversed_value_dataquest_1 = []
for i in range(len(values)):
    reversed_value_dataquest_1.append(values[-i:])
   
reversed_value_dataquest_1

To your first question:
This list(range(0, -10, -1)) is counting values from -10 to -1. This is how range works normally, counting values between intervals. This is not the same as accessing the index positions or reversing a list. Here, you are actually creating a new list from range.

2 Likes

Documentation about slicing syntax is here but i found this helpful more

2 Likes

Hi, Im not understanding why we have to use range(len(values)):
instead of just going straight to
for i in values:
reverese_values.append(reverse(i))??
Im new to coding if anyone could help, thank you

3 Likes

Hi @chautran0729:

Why not you try to run the code you suggested and tell me what results it gives? If it is an error, why do you think the error occurred? This is part and parcel of the learning process so take some time to reflect on the 2 questions as mentioned above.

In python there is usually more than 1 way to do something. You can still have an alternative method using reverse() which I just tried out. How do you think the code should be like in order for this to work effectively? Here is the documentation for your reference.

2 Likes

sorry about the confusion I was asking about:
range(len(values))
Im not understanding what is the use and practice of this is

You can read about the range() documentation here.

for i in values:
for i in range(len(values)):

these essentially do the same thing (in terms of iterating over the values, which you are trying to accomplish). The first one iterates over each value. The second one calculates the length of the values list (in this case 14) before iterating over the list (index 0 to index 13–remember python is zero-indexed). It does not matter which one you use in this example.

2 Likes

By the way this is the alternative code I was talking about earlier

values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 17, 14, 1, 9]
values.reverse()
reversed_values = values
2 Likes

When I google and found reverse () function for list, however I wonder why below my code returns “None” value:
reversed_values=values.reverse()
print(reversed_values)

Output

None

@tranmaianh.hcm this should be done in place (using reverse()), and therefore there is no need to try to reassign it to another variable. In my method slicing is not in place so assignment is needed.

1 Like

Lists and For Loops Practice Problems, 6. Reversing A List

I may have a better solution than the one provided:

`values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 17, 14, 1, 9]

reversed_values =
for i in reversed(values):
reversed_values.append(i)`

Note: I know this doesn’t answer your question, but I think others have sufficiently done that already.

managed to do it without range(am I the only one who didnt get ‘range’ lesson on Python Basics for Data Analysis?)

values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 17, 14, 1, 9]

new_values = []
number = 0

for char in values:
    number = number - 1
    new_values.append(values[number])

values = new_values
print(values)