Reputation: 519
i have list
my_list = ['Cat Dog Tiger Lion', 'Elephant Crocodile Monkey', 'Pantera Cat Eagle Hamster']
i need to delete all of elements which containing sub = 'Cat'
so i need to have output
['Elephant Crocodile Monkey']
i think i need to do something with regex, maybe re.compile
to find this values and then drop them from my_list
?
i have been tryed:
for string in my_list:
if string.find(sub) is not -1:
print("this string have our sub")
else:
print("sthis string doesnt have our sub")
but i dont know how to combine it with delete this rows from my_list
Upvotes: 0
Views: 69
Reputation: 111
Here's your list:
my_list = ['Cat Dog Tiger Lion', 'Elephant Crocodile Monkey', 'Pantera Cat Eagle Hamster']
You can use list comprehension for filtering the list:
new_list = [x for x in my_list if(not x.__contains__('Cat'))]
Upvotes: 2
Reputation: 88305
To filter lists, a simple approach is to use a list comprehension:
[i for i in my_list if 'Cat' not in i]
# ['Elephant Crocodile Monkey']
Which is equivalent to:
out = []
for i in my_list:
if 'Cat' not in i:
out.append(i)
Upvotes: 2