I was trying to find range and steps that would be applied while slicing a list in python. An easy way to do it is if we know the start and end index and steps provided then we can use slices.index method to do so. For e.g Creating a slice object and using indices method on top of it returns me a tuple with start, stop and step values.
slice(start,stop,step).indices(length of index) -> ( start, end, step)
>>> slice(10,-5,-1).indices(6)
(5, 1, -1)
>>>
There are cases where these start and end indexes in above e.g 10 and 5 passed to create object slice are missing, so for such scenarios what would be the appropriate replacement for these values while creating slice object ?
>>> seq='python'
>>> seq[::-1]
Note i understand there are rules around how to calculate i and j when the steps are positive or negative. E.g
If i and j is omitted and step is > 0 then i is 0 and j is len(seq) while if step is -1 then i is len(seq)-1 and j is -1
As per this rule in below e.g i=5 and j=-1. Ideally -1 is end index ( not sure if rules gets changed when steps are positive vs negative ) so this should have not reversed the string.I tried to substitute this with the hardcode values and it results me and empty sequence which is exactly what i expected.
>>> seq='python'
>>> seq[::-1]
'nohtyp'
>>> seq[5:-1:-1]
''
So how does this reverse string works, I wanted to check if i can used slice.index method to get the values but I am not sure how to substitute start and end while creating a slice object for missing i and j.
Thanks