Get the indices of word sequences in texts represented as list of words

Given a paragraph represented as a list of words:

context = ['Katie', 'Joplin', 'is', 'an', 'American', 'sitcom', 'created', 'by', 'Tom', 'Seeley', 'and', 'Norm', 'Gunzenhauser', '.', 'The', 'sitcom', 'received', 'positive', 'reviews', 'thanks', 'to', 'the', 'brilliance', 'of', 'Tom', 'Seeley', '.']

And a list of multi-word target strings:

target = ['sitcom created', 'Tom Seeley']

How to get the indices of the multi word targets?

In this case answer should be:

[[5, 6], [8, 9], [24, 25]]

Answer

If context contains only single words (without spaces), you can join the context to single string and then use str.index:

target = ["sitcom created", "Tom Seeley"]

out, joined = [], " ".join(context)
for t in target:
    try:
        idx = joined.index(t)
        cnt = joined[:idx].count(" ")
        out.append([cnt, cnt + t.count(" ")])
    except:
        continue

print(out)

Prints:

[[5, 6], [8, 9]]

EDIT: For multiple occurrences:

target = ["sitcom created", "Tom Seeley"]

out, joined = [], " ".join(context)
for t in target:
    idx = 0
    while True:
        try:
            idx_new = joined[idx:].index(t)
            cnt = joined[: idx + idx_new].count(" ")
            out.append([cnt, cnt + t.count(" ")])
            idx += idx_new + len(t)
        except:
            break

print(out)

Prints:

[[5, 6], [8, 9], [24, 25]]