Try the following implementation
>>> def foo(inlist, delim = ''): start = 0 try: while True: stop = inlist.index(delim, start) yield inlist[start:stop] start = stop + 1 except ValueError:
Another possible implementation might be itertools.groupby . But then you have to filter the result to remove ['']. But although it may look one-line, the above implementation is more pythonic, because its intuitive and readable
>>> from itertools import ifilter, groupby >>> list(ifilter(lambda e: '' not in e, (list(v) for k,v in groupby(inlist, key = lambda e:e == '')))) [['a', 'b', 'c'], ['d', 'e', 'f', 'g'], ['d', 's', 'd', 'a']]
source share