I have a list of strings. I'm trying to remove elements that have certain substrings. If I use the following, only elements containing 'Cantab' are removed:
for line in merged:
if 'Duke' in line and 'Sir' not in line or 'Cantab' in line or 'Rick' in line:
merged.remove(line)
If I try to break up the conditional, my target results aren't achieved, but I successfully remove elements that contain Duke, but not Sir Duke.:
if 'Duke' in line and 'Sir' not in line:
merged.remove(line)
This works as expected:
if 'Duke' in line and 'Sir' not in line:
merged.remove(line)
elif 'Cantab' in line:
merged.remove(line)
But the following only removes elements containing 'Cantab'!!!:
if 'Duke' in line and 'Sir' not in line:
merged.remove(line)
elif 'Cantab' in line:
merged.remove(line)
elif 'Rick' in line:
merged.remove(line)
I'm having trouble figuring out the logic here. Thanks!