As an alternative answer, you can do this with itertools and use the groupby function to group your list, and also use combination to create a pair index list to group the key: ( i<=word.index(x)<=j ) and, finally use set to get a unique list.
Also note that you can get a unique combination of the pair index first by this method, which when you have pairs, such as (i1,j1) and (i2,j2) , if i1==0 and j2==3 and j1==i2 as (0,2) and (2,3) , this means that the results of these slices are the same, you need to delete one of them.
All in one list comprehension:
subs=[[''.join(i) for i in j] for j in [[list(g) for k,g in groupby(word,lambda x: i<=word.index(x)<=j)] for i,j in list(combinations(range(len(word)),2))]] set([' '.join(j) for j in subs]) # set(['WIN E', 'W IN E', 'W INE', 'WI NE', 'WINE'])
Demonstration in detail:
>>> cl=list(combinations(range(len(word)),2)) >>> cl [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] >>> new_l=[[list(g) for k,g in groupby(word,lambda x: i<=word.index(x)<=j)] for i,j in cl] >>> new_l [[['W', 'I'], ['N', 'E']], [['W', 'I', 'N'], ['E']], [['W', 'I', 'N', 'E']], [['W'], ['I', 'N'], ['E']], [['W'], ['I', 'N', 'E']], [['W', 'I'], ['N', 'E']]] >>> last=[[''.join(i) for i in j] for j in new_l] >>> last [['WI', 'NE'], ['WIN', 'E'], ['WINE'], ['W', 'IN', 'E'], ['W', 'INE'], ['WI', 'NE']] >>> set([' '.join(j) for j in last]) set(['WIN E', 'W IN E', 'W INE', 'WI NE', 'WINE']) >>> for i in set([' '.join(j) for j in last]): ... print i ... WIN E W IN E W INE WI NE WINE >>>