def substrings_in_set(s, words):
    if s in words:
        yield [s]
for i in range(1, len(s)):
    if s[:i] not in words:
        continue
for rest in substrings_in_set(s[i:], words):
    yield [s[:i]] + rest
​Breaking a string into individual words(Python)