I have a list of items : eg:
a = [\'IP 123 84\', \'apple\', \'mercury\', \'IP 543 65\', \'killer\', \'parser\', \'goat\',
\'IP 549 54 pineapple\', \'dja
Using a generator.
def merge(x, key='IP'):
tmp = []
for i in x:
if (i[0:len(key)] == key) and len(tmp):
yield ' '.join(tmp)
tmp = []
tmp.append(i)
if len(tmp):
yield ' '.join(tmp)
a = ['IP 123 84','apple','mercury','IP 543 65','killer','parser','goat','IP 549 54 pineapple','django','python']
print list(merge(a))
['IP 123 84 apple mercury', 'IP 543 65 killer parser goat', 'IP 549 54 pineapple django python']