from itertools import accumulate, groupby
from operator import itemgetter
x = ['!test','hello','world','!echo','!embed','oh god']
cumsum = accumulate(map(lambda s: s.startswith('!'), x))
result = ['\n'.join(map(itemgetter(0), g)) for _, g in groupby(zip(x, cumsum), itemgetter(1))]
print(result)
ZnJvbSBpdGVydG9vbHMgaW1wb3J0IGFjY3VtdWxhdGUsIGdyb3VwYnkKZnJvbSBvcGVyYXRvciBpbXBvcnQgaXRlbWdldHRlcgoKeCA9IFsnIXRlc3QnLCdoZWxsbycsJ3dvcmxkJywnIWVjaG8nLCchZW1iZWQnLCdvaCBnb2QnXQoKY3Vtc3VtID0gYWNjdW11bGF0ZShtYXAobGFtYmRhIHM6IHMuc3RhcnRzd2l0aCgnIScpLCB4KSkKcmVzdWx0ID0gWydcbicuam9pbihtYXAoaXRlbWdldHRlcigwKSwgZykpIGZvciBfLCBnIGluIGdyb3VwYnkoemlwKHgsIGN1bXN1bSksIGl0ZW1nZXR0ZXIoMSkpXQoKcHJpbnQocmVzdWx0KQ==