Jan-03-2019, 09:06 AM
Here is a way, using module itertools
>>> import itertools as itt >>> L = ['visit', 'houston', 'and', 'san', 'antonio', 'and', 'austin', 'and', 'corpus', 'christi'] >>> [list(g) for k, g in itt.groupby(L, key=lambda word: word=='and') if not k] [['visit', 'houston'], ['san', 'antonio'], ['austin'], ['corpus', 'christi']]Avoid using 'list' as a variable name, it is the name of a built-in function.