Hello I have a list of strings and a list of prefixes. I want to remove all elements from the list of strings that start with a prefix from the list of prefixes.
I used a for loop but it doesn’t seem to work. Any idea?
list_of_strings = ['test-1: foo', 'test-2: bar', 'test-3: cat']
list_of_prefixes = ['test1', 'test-2']
final_list = []
for i in list_of_strings:
for j in list_of_prefixes:
if not i.startswith(j):
final_list.append(i)
print(list(set(final_list)))
# ['test-3: cat', 'test-1: foo', 'test-2: bar']
The expected final output I want to get is: final_list = ['test-3: cat']
New contributor
sjko5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.