Baz
Baz

Reputation: 13185

Filter in list comprehension

Is it possible to add a condition to this list comprehension so that its result does not contain empty strings:

words = [regex.sub('\P{alpha}','',word) for word in words]

Upvotes: 0

Views: 1376

Answers (3)

Paulo Almeida
Paulo Almeida

Reputation: 8071

You can check for alpha characters in the word:

[regex.sub('\P{alpha}','',word) for word in words if list(filter(str.isalpha, word))]

This can already be faster than the other approaches (it depends on whether there are words that become empty strings), but then you might as well not use the regular expression:

[x for x in ("".join(filter(str.isalpha, word)) for word in words) if x]

This is quite faster (tested on Python 2.7) and, in my opinion, doesn't hurt readability much, although it is a bit uglier than in Python 2.7, where I originally tested.

Upvotes: 0

chepner
chepner

Reputation: 532428

You'll have to post-process the resulting list (and convert the result to a list, per Ashwini's comment):

words = list(filter(None, (regex.sub('\P{alpha}','',word) for word in words)))

You can also pass your original list comprehension as the 2nd argument:

words = filter(None, [regex.sub('\P{alpha}','',word) for word in words])

The first is probably more efficient if you expect many of the substitutions to produce empty strings.


Here's a solution using itertools and functools, for functional-style fans:

from itertools import imap, filter
from functools import partial
modifier = partial(regex.sub, '\P{alpha}', '')
words = list(ifilter(None, imap(modifier, words)))

Upvotes: 3

John La Rooy
John La Rooy

Reputation: 304473

Move it into a generator expression and do a list comprehension over that.

words = [x for x in (regex.sub('\P{alpha}', '', word) for word in words) if x]

Upvotes: 4

Related Questions