2

I have a server_list and I am parsing email addresses from a file. I want to collect all addresses that are not from certain servers e.g. gmail.com.

Currently I have this inside the file reading loop:

server_list = ["gmail.com", "yahoo.com"] #etc
for servers in server_list:
   if servers in emailaddress: #get emailaddress from inside of a open(file) line loop
        badmail.extend(emailaddress)

This allows me to collect the bad emails in a list badmail. Is there any way to create a list of good emails i.e. if emailaddress is not contained in any items in server_list in the same loop or do I have to create a list of all emails and remove the bad emails?

4
  • Sounds like you could just handle that in the else Commented Apr 28, 2014 at 10:26
  • what is emailaddress? A list? Commented Apr 28, 2014 at 10:27
  • You could simplify to if any(servers in emailaddress for servers in server_list):. Also, you probably want to append, not extend, the list, assuming it's a string. Commented Apr 28, 2014 at 10:27
  • emailaddress is a string extracted from a line in the file Commented Apr 28, 2014 at 10:29

1 Answer 1

4

You can use all function to make sure that the emailaddress doesn't end with any of the servers in the server_list, like this

server_list, good_email = ["gmail.com", "yahoo.com"], []
if all(not emailaddress.endswith(server) for server in server_list):
    good_email.append(emailaddress)

The same way, you can use any function to get the bad email address, like this

server_list, bad_email = ["gmail.com", "yahoo.com"], []
if any(emailaddress.endswith(server) for server in server_list):
    bad_email.append(emailaddress)

Looks like you are reading the email addresses from a file. So, you can do something like this

server_list, good_list, bad_list = ["gmail.com", "yahoo.com"], [], []
with open("email.txt") as in_file:
    for email_address in in_file:
        email_address = email_address.rstrip()
        if any(email_address.endswith(server) for server in server_list):
            bad_list.append(email_address)
        else:
            good_list.append(email_address)

As per lvc's suggestion, we can actually pass a tuple of data to str.endswith. So, the code can be further simplified to

server_list, good_list, bad_list = ("gmail.com", "yahoo.com"), [], []
...
...
if email_address.endswith(server_list):
    bad_list.append(email_address)
else:
    good_list.append(email_address)
Sign up to request clarification or add additional context in comments.

5 Comments

+1, but you should just use if/else instead of checking the same thing twice.
@tobias_k Thanks :) I was editing the answer, please check now.
You could use email_address.endswith(tuple(server_list)) rather than a genexp, or even make server_list a tuple to start with.
thank you, this is very useful. However I notice you have made it to check only the ends - is there any way to check all parts too? i.e. like .contains?
@Kash That is simple :) You can do any(server in email_address for server in server_list)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.