I want to split a list names element. More precicely i only want to split the strings with Oscar Muller
names = ['Oscar Muller Some other Name', 'Oscar Muller', 'Peter Pan']
expected_names = ['Oscar Muller', 'Some other Name', 'Oscar Muller', 'Peter Pan']
d = "Oscar Muller "
for line in names:
s = [e+d for e in line.split(d) if e]
That didnt do anything.
[list(filter(None, re.split(r'Oscar\sMuller\s', i))) for i in names]
didnt do anything either.
d1 = re.compile(r"Oscar\sMuller\s")
d = d1.search(names)
for line in names:
if d:
s = [e+d for e in line.split(d) if e]
but it caused issues with input .split(). Error: TypeError: must be str or None, not re.Pattern. So i changed it to process each list element.
d1 = re.compile(r"Oscar\sMuller\s")
d = list(filter(d1.match, names))
for line in names:
if d:
s = [e+d for e in line.split(d) if e]
But it didnt work either, returning TypeError: must be str or None, not list
Question: What am i doing wrong?