If we have a list of strings in python and want to create sublists based on some special string how should we do?
For instance
reduce comes to mind:
def split(iterable, where):
def splitter(acc, item, where=where):
if item == where:
acc.append([])
else:
acc[-1].append(item)
return acc
return reduce(splitter, iterable, [[]])
data = ["data","more data","","data 2","more data 2","danger","","date3","lll"]
print split(data, '')
Result:
[['data', 'more data'], ['data 2', 'more data 2', 'danger'], ['date3', 'lll']]