Having a list
that contains string
s, I want to split the list
at each point where it contains the empty string
(''
), e.g.
['this', 'is', '', 'an', 'example']
should become
[['this', 'is'], ['an', 'example']]
I wrote a generator that does this:
def split(it, delimiter):
it = iter(it)
buffer = []
while True:
element = next(it)
if element != delimiter:
buffer.append(element)
elif buffer:
yield buffer
buffer = []
Since this looks pretty general, I was wondering if I missed some similar function or related pattern in itertools
or somewhere else...?