Just use itertools.combinations
from itertools import combinations text = "the big fat cat sits on the mat eating a rat" lst = text.split() for start, end in combinations(range(len(lst)), 2): print lst[start:end+1]
output:
['the', 'big'] ['the', 'big', 'fat'] ['the', 'big', 'fat', 'cat'] ['the', 'big', 'fat', 'cat', 'sits'] ['the', 'big', 'fat', 'cat', 'sits', 'on'] ['the', 'big', 'fat', 'cat', 'sits', 'on', 'the'] ['the', 'big', 'fat', 'cat', 'sits', 'on', 'the', 'mat'] ['the', 'big', 'fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating'] ['the', 'big', 'fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating', 'a'] ['the', 'big', 'fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating', 'a', 'rat'] ['big', 'fat'] ['big', 'fat', 'cat'] ['big', 'fat', 'cat', 'sits'] ['big', 'fat', 'cat', 'sits', 'on'] ['big', 'fat', 'cat', 'sits', 'on', 'the'] ['big', 'fat', 'cat', 'sits', 'on', 'the', 'mat'] ['big', 'fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating'] ['big', 'fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating', 'a'] ['big', 'fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating', 'a', 'rat'] ['fat', 'cat'] ['fat', 'cat', 'sits'] ['fat', 'cat', 'sits', 'on'] ['fat', 'cat', 'sits', 'on', 'the'] ['fat', 'cat', 'sits', 'on', 'the', 'mat'] ['fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating'] ['fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating', 'a'] ['fat', 'cat', 'sits', 'on', 'the', 'mat', 'eating', 'a', 'rat'] ['cat', 'sits'] ['cat', 'sits', 'on'] ['cat', 'sits', 'on', 'the'] ['cat', 'sits', 'on', 'the', 'mat'] ['cat', 'sits', 'on', 'the', 'mat', 'eating'] ['cat', 'sits', 'on', 'the', 'mat', 'eating', 'a'] ['cat', 'sits', 'on', 'the', 'mat', 'eating', 'a', 'rat'] ['sits', 'on'] ['sits', 'on', 'the'] ['sits', 'on', 'the', 'mat'] ['sits', 'on', 'the', 'mat', 'eating'] ['sits', 'on', 'the', 'mat', 'eating', 'a'] ['sits', 'on', 'the', 'mat', 'eating', 'a', 'rat'] ['on', 'the'] ['on', 'the', 'mat'] ['on', 'the', 'mat', 'eating'] ['on', 'the', 'mat', 'eating', 'a'] ['on', 'the', 'mat', 'eating', 'a', 'rat'] ['the', 'mat'] ['the', 'mat', 'eating'] ['the', 'mat', 'eating', 'a'] ['the', 'mat', 'eating', 'a', 'rat'] ['mat', 'eating'] ['mat', 'eating', 'a'] ['mat', 'eating', 'a', 'rat'] ['eating', 'a'] ['eating', 'a', 'rat'] ['a', 'rat']