How does the stride work in list slicing?


#1

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

FAQ: Learn Python - Advanced Topics in Python - List Slicing Syntax