sentence = 'Two Dogs and Three Cats'
list_of_indices = [[0,3], [13, 18]]
I wish to remove multiple indices from a string based on its index position by referencing a nested list of index values (which may have more than 2 elements).
I am able to remove a single index range from the string (e.g remove the word Two from the sentence using S = sentence[:0] + sentence[3:] but how do I modify this code to remove multiple range of indices by referencing list_of_indices?
I tried one other method:
to_remove = []
for i in list_of_indices:
word = sentence[i[0]: i[1]]
to_remove.append(word)
resultwords = [word for word in sentence.split() if word not in to_remove]
print(resultwords)
result = ' '.join(resultwords)
print(result)
However, it would not work if the word contains other symbols in the sentence e.g. for sentence = ':Two: Dogs and Three Cats', it would not remove Two
list_of_indiceslist_of_indicesbe a sorted list with no overlapping indices?