fork download
  1. from itertools import accumulate, groupby
  2. from operator import itemgetter
  3.  
  4. x = ['!test','hello','world','!echo','!embed','oh god']
  5.  
  6. cumsum = accumulate(map(lambda s: s.startswith('!'), x))
  7. result = ['\n'.join(map(itemgetter(0), g)) for _, g in groupby(zip(x, cumsum), itemgetter(1))]
  8.  
  9. print(result)
Success #stdin #stdout 0.03s 9360KB
stdin
Standard input is empty
stdout
['!test\nhello\nworld', '!echo', '!embed\noh god']