Home > Mobile >  Filtering/removing words given a pattern from a list
Filtering/removing words given a pattern from a list

Time:10-14

I have a list. Many of the words in the list start with 'JKE0'. I want to remove all words that start with this pattern.

This is what i've done but it's failing, the list remains the same size and nothing is removed

new_list = list(x)
r = re.compile('JKE0')
rslt = list(filter(lamda a: (a != r.match), new_list))

CodePudding user response:

No need for a regex:

rslt = [w for w in new_list if not w.startswith('JKE0')]

If you really want to use a regex:

r = re.compile('JKE0')
rslt = [w for w in new_list if not r.match(w)]
  • Related