I want to split a string by a list of indices, where the split segments begin with one index and end before the next one.
Example:
You can write a generator if you don't want to make any modifications to the list of indices:
>>> def split_by_idx(S, list_of_indices):
... left, right = 0, list_of_indices[0]
... yield S[left:right]
... left = right
... for right in list_of_indices[1:]:
... yield S[left:right]
... left = right
... yield S[left:]
...
>>>
>>>
>>> s = 'long string that I want to split up'
>>> indices = [5,12,17]
>>> [i for i in split_by_idx(s, indices)]
['long ', 'string ', 'that ', 'I want to split up']