tags:

views:

51

answers:

1

I want to filter strings in a list based on a regular expression.

Is there something better than [x for x in list if r.match(x)] ?

+4  A: 
filter(r.match, list)
sepp2k
Actually, list comprehensions are usually prefered over functional constructs such as filter, reduce, lambda, etc.
Ivo van der Wijk
@Ivo: They are usually preferred because they're usually clearer and often more succinct. However in this case, the `filter` version is perfectly clear and has much less noise.
sepp2k
well, my question was if there's something like random.choice, but for regexps, but the answer seems no :-)
leoluk