Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list comprehension logic error

I'm trying to weed out strings that contains "msi" using regular expressions and a list comprehension. However, when I print the list, strings that contain "msi" are still in the list. What exactly would the error be? This is my code:

spam_list = [l for l in spam_list if not re.match("msi", l)]
like image 533
nobody Avatar asked Dec 02 '22 02:12

nobody


1 Answers

re.match() matches from the beginning of the string. Use re.search(), or even better, in.

L = [l for l in L if "msi" not in l]
like image 50
Ignacio Vazquez-Abrams Avatar answered Dec 05 '22 02:12

Ignacio Vazquez-Abrams