Reputation: 2588
Right now I'm "removing" emails from a list by mapping a new list excluding the things I don't want. This looked like:
pattern = re.compile('b\.com')
emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
emails = [e for e in emails if pattern.search(e) == None]
# resulting list: ['[email protected]', '[email protected]']
However, now I need to filter out multiple domains, so I have a list of domains that need to be filtered out.
pattern_list = ['b.com', 'c.com']
Is there a way to do this still in list comprehension form or am I going to have to revert back to nested for loops?
Note: splitting the string at the @ and doing word[1] in pattern_list
won't work because c.com
needs to catch sub.c.com
as well.
Upvotes: 2
Views: 178
Reputation: 251598
There are a few ways to do this, even without using a regex. One is:
[e for e in emails if not any(pat in e for pat in pattern_list)]
This will also exclude emails like [email protected]
and [email protected]
, but so does your original solution. It does not, however, exclude cases like user@bocom
, which your existing solution does. Again, it's not clear if your existing solution actually does what you think it does.
Another possibility is to combine your patterns into one with rx = '|'.join(pattern_list)
and then match on that regex. Again, though, you'll need to use a more complex regex if you want to only match b.com
as a full domain (not as just part of the domain or as part of the username).
Upvotes: 2
Reputation: 1473
import re
pattern = re.compile('b.com$|c.com$')
emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
emails = [e for e in emails if pattern.search(e) == None]
print emails
what about this
Upvotes: 2