218
218

Reputation: 1822

Increment slice object?

Is it possible to change the values in a python slice object?

For example, if I have

slice(0,1,None)

How would I, in effect, add 1 to the start and end values and so convert this to:

slice(1,2,None)

Upvotes: 2

Views: 503

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95992

Not exactly elegant, but it works:

>>> s1 = slice(0,1,None)
>>> s2 = slice(s1.start + 1, s1.stop + 1, s1.step)
>>> s2
slice(1, 2, None)
>>> 

Upvotes: 4

Related Questions