Python split() without removing the delimiter

some1 picture some1 · Oct 23, 2011 · Viewed 94.1k times · Source

This code almost does what I need it to..

for line in all_lines:
    s = line.split('>')

Except it removes all the '>' delimiters.

So,

<html><head>

Turns into

['<html','<head']

Is there a way to use the split() method but keep the delimiter, instead of removing it?

With these results..

['<html>','<head>']

Answer

P.Melch picture P.Melch · Oct 23, 2011
d = ">"
for line in all_lines:
    s =  [e+d for e in line.split(d) if e]