None can be explicitly provided to indicate "to the end" (for a negative step, "end" is the beginning of the sequence):
lst[:ind - 1 if ind else None:-1]
While you said you were avoiding a two-step approach, it was, frankly, easier to do so; if you don't expect the chunk to be huge, simplicity will be worth any trivial performance loss that you might suffer:
lst[ind:][::-1]
For recording on trivial microobjects, assuming each ind value is equally common, the one-step approach is faster, but the difference is pretty small, if only your list is huge. For example, for your four list items using ipython for microdetection:
>>> lst = [1, 2, 3, 4] >>> %%timeit -r5 inds = range(len(lst)) ... for ind in inds: ... lst[:ind-1 if ind else None:-1] ... 1000000 loops, best of 5: 791 ns per loop >>> %%timeit -r5 inds = range(len(lst)) ... for ind in inds: ... lst[ind:][::-1] ... 1000000 loops, best of 5: 1.1 µs per loop
This is slower, but the cost is only about 300 ns. Even when lst is len 4000, the difference is 18 versus 35.5 ms per cycle; which almost doubles the time, but if it is not performance critical (or lists are usually smaller), I would call it acceptable, since it eases the load on the media to read "a fragment from ind to the end, and then vice versa", and no more complex single-stage cut structure.
Shadowranger
source share