Q: Finding subsentences from one singular sentence I am trying to create a list of possible subsentences from a single given sentence in Python, but cannot figure out how to do so.
For example:
sentence_1 = 'the dog jumped around'

and I want to split it into:
['the',
 'the dog'
 'dog jumped'
 'jumped around'
 'the dog jumped'
 'dog jumped around'
 'the dog jumped around'
 ]


A: To have all subsentence of at least 2 words:
sentence_1 = 'the dog jumped around'

words = sentence_1.split()

data = []
for i in range(len(words)):
    for j in range(i + 2, len(words) + 1):
        data.append(" ".join(words[i:j]))
print(data)

As you also want the first word:
data.append(words[0])

Output:
['the', 'the dog', 'the dog jumped', 'the dog jumped around', 'dog jumped', 'dog jumped around', 'jumped around']

I have to admit I don't really understand why 'the' has to be in the list, but not 'dog', 'jumped' nor 'around'

To have all subsentences:
sentence_1 = 'the dog jumped around'

words = sentence_1.split()

data = []
for i in range(len(words)):
    for j in range(i + 1, len(words) + 1):
        data.append(" ".join(words[i:j]))
print(data)

Output:
['the', 'the dog', 'the dog jumped', 'the dog jumped around', 'dog', 'dog jumped', 'dog jumped around', 'jumped', 'jumped around', 'around']

