Kamikaze_goldfish
Kamikaze_goldfish

Reputation: 861

How do I extract specific emails from list in python?

I have a huge list of emails and I have tried to extract only the good emails. The problem is that the domains are many since they might have some custom domain, in addition to the standard gmail domains. I am trying to take out the corporate domains from the list. Here is an example of my code. When I run the below code, I get all the emails in the list.

data = ['[email protected]','[email protected]', '[email protected]', '[email protected]']

#I want to remove the domains with statefarm.com or edwardjones.com
for email in data:
    if "statefarm.com" not in email or "edwardjones.com" not in email:
    # I have even tried but it still hasn't worked. 
    #if "statefarm.com" or "edwardjones.com" not in email:
        print(email)

Upvotes: 0

Views: 230

Answers (1)

U13-Forward
U13-Forward

Reputation: 71600

As @djukha says, replace or to and, so do:

data = ['[email protected]','[email protected]', '[email protected]', '[email protected]']
for email in data:
    if "statefarm.com" not in email and "edwardjones.com" not in email:
        print(email)

But for even better:

data = ['[email protected]','[email protected]', '[email protected]', '[email protected]']
print('\n'.join(filter(lambda x: any(i in x for i in {"statefarm.com","edwardjones.com"}),data)))

Upvotes: 1

Related Questions