newbzzs
newbzzs

Reputation: 315

Filtering/removing words given a pattern from a list

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))

Upvotes: 0

Views: 171

Answers (1)

mozway
mozway

Reputation: 262359

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)]

Upvotes: 1

Related Questions