How does the stride work in list slicing?

Question

How does the stride work in list slicing?

Answer

When we want a slice of a list, we can specify the start, end, and stride. The stride is 1 by default and will go through each number in the range. If you change the number, then that’s by how many it’ll change each time. Take a look at the example below for a better understanding:

my_name = “BobbyTarantino”

print my_name[::1]  #BobbyTarantino
print my_name[::-1 ]#onitnaraTybboB

print my_name[::2]  #Bbyaatn
print my_name[::-2] #oinrTbo
5 Likes
l = [i ** 2 for i in range(1, 11)]
# Should be [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

print l[2:9:2]

With the above code, the start point is 2 and ends at 9 with a stride of 2 ( thats if am right)
But when run this code the outcome is as shown below

[9, 25, 49, 81]
[9, 25, 49, 81]
[9, 25, 49, 81]
[9, 25, 49, 81]
[9, 25, 49, 81]

Can someone help explain the logic and syntax flow for me Please!!!
Am a bit thrown off

@iamstyles The reason it’s printing the result 5 times is probably because each check the SCT (Submission Correctness Test) makes re-runs your code. If you run it in a Python console locally, it’ll only print the list once.

3 Likes

@iamstyles The default starting index is 0, that means it starts at index 2 inclusive which has a value of 9, it ends at index 9 exclusive (-> index 8 inclusive) which has a value of 81, with a stride of 2 which refer to index 4 and 6 having 25 and 49 as values. The result should normally be displayed in 1 line only.

7 Likes

Even i don’t understand it. why does it print " [9, 25, 49, 81] " and not " [1, 9, 25, 49, 81] " ? Please tell me if u find the ans.

print l[2:9:2]

That’s why. It computes correctly, but the print statement is a slice that excludes the 1.

The console prints [9, 25, 49, 81] because these are the values that correspond to the output for the slice chosen (l[2:9:2]).

As the start point is inclusive, l[2] would refer to position 2 in range(1, 11). Position 0 is 1, position 1 is 2, so position 2 should be 3. This is why the output starts at 9 (or 3**2).

I see why the peeps are getting confused. Look at the below and tell me where does it say it refers to an index? Actually there is no such a word there other than the last sentence and half that “explains” how it works.

Where start describes where the slice starts (inclusive), end is where it ends (exclusive), and stride describes the space between items in the sliced list. For example, a stride of 2 would select every other item from the original list to place in the sliced list.

I was confused with this too until I read paul_catarig comment that pointed out the fact that is referring to a position rather than “every other” or “space between items”.

These descriptions/ terminologies could be a bit more specific.

Too many learners are complaining that they do not know what an index is. How is it then that they understand range()? Why would anyone simply refuse to search for concepts only to complain in the forums of CC’s shortcomings? Who is the one responsible for our education?

3 Likes

I think you misunderstand what I was trying to say back there. Besides Im not complaining as you assumed it wrongly. I had my objection there which is completely valid since anyone on this page can add any sort of objection.

With all due respect, where did you get these assumptions from?

To answer your questions, (which was probably rhetorical) the “They” is unknown for me but Im trying to learn here the whole concept of coding etc… Me personally, I have chosen Codecademy because I was told " this is the best site". I don’t refuse to learn elsewhere nor I refuse to “search other concepts”. Please read my comment again regarding the “index” stuff, Im not dissing the site, nor you nor anyone but making suggestion, that is all.

Have a good day and take care of yourself.

3 Likes

Please can anyone say me the logic behind the list slicing?
I can’t understand by syntax

A slice is a virtual segment of an iterable object such as a string, a list, or a tuple. All these objects are subscriptable, meaning we can access their elements by index.

A slice, then, is a subscripted range within a sequence. It has two positional arguments that are start and end indices, and a third that specifies the stride and stride direction.

How are we doing, so far?

2 Likes

Thank you very much!!!

1 Like