Do I need to include the start and end indexes in my backwards_by_tens slice?

Question

Do I need to include the start and end indexes in my backwards_by_tens slice?

Answer

Nope! Since we want to continue going backwards through the to_one_hundred list until we reach the beginning, there’s no need to fill in the start or end indexes, just the stride.
Recall that writing my_list[::-1] will traverse through the entire list backwards.

1 Like

Hello! For this exercise, I wanted to try adding start and end indeces just for practice, to make sure I understood.

The solution wanted by the exercise, “stride length”:

exercise: Create a variable, backwards_by_tens , and set it equal to the result of going backwards through to_one_hundred by tens. Go ahead and print backwards_by_tens to the console.

to_one_hundred = range(101)
backwards_by_tens = to_one_hundred[::-10]
print backwards_by_tens
prints
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]

If I try to go from first negative to first positive index, the 0 value doesn’t print even though no index goes lower than 0:

backwards_by_tens = to_one_hundred[-1:0:-10]
print backwards_by_tens
it prints [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

If I try to go from first negative index to what I think is the last negative index, the 0 value also doesn’t print

backwards_by_tens = to_one_hundred[-1:-101:-10]
print backwards_by_tens
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

it only prints 0 when I go to -102 (in to_one_hundred[-1:-102:-10]), which I guess makes sense if there are 102 values from 0 to 101. Is this a side effect of the negative index starting from -1 instead of from a 0? I’m not sure how to count indeces when going backwards. Thank you in advance for your help.

1 Like

for this exercise you just change in stride with the -10 and not to change another like this backwards_by_tens = to_one_hundered[::-10]

this function make value from 100 to 0 because stride traverses by 10

Remember that you aren’t specifying values (start at this number and end when you get to that number). You are specifying indices (start at this index and end at that index).

I honestly don’t know how specifying -1 as the starting place got you anything at all, but that’s beyond my knowledge, since I am just a student.

In this type of python code, your specified starting index will be included, but your ending index is NOT supposed to be included. By saying that you want to end at index 0, you are actually saying to end one BEFORE index 0, hence it not being included in your results. I tried ending at index -1, but it just returned an empty list.

Here’s a setup that may seem more intuitive. The first one goes from index 60 till there are no more indices, the second one specifies to stop just before index 0. In this case, I don’t think it is possible to specify an endpoint AND include index 0. Because there are no negative indices in python (I assume), leaving it unspecified is the only way to actually accomplish counting backward to index zero while also including index zero.

to_one_hundred = range(101)

backwards_by_tens = to_one_hundred[60::-10]
backwards_by = to_one_hundred[60:0:-10]

print backwards_by_tens
print backwards_by

Result:

image