python - more efficient way to replace items on a list based on a condition -


i have following piece of code. basically, i'm trying replace word if matches 1 of these regex patterns. if word matches once, word should gone new list. code below works, however, i'm wondering if there's way implement can indefinitely add more patterns 'pat' list without having write additional if statements within loop.

to clarify, regex patterns have negative lookaheads , lookbehinds make sure it's 1 word.

pat = [r'(?<![a-z][ ])pacific(?![ ])', r'(?<![a-z][ ])global(?![ ])']  if isinstance(x, list):     new = []     in x:         if re.search(pat[0], i):             = re.sub(pat[0], '', i)         if re.search(pat[1], i):             = re.sub(pat[1], '', i)         if len(i) > 0:             new.append(i)     x = new  else:     x = x.strip() 

just add for loop:

for patn in pat:     if re.search(patn, i):          = re.sub(patn, '', i) if i:     new.append(i) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -