Take the 2-minute tour ×

I'm trying to write some code to compare two lists where each list contains email addresses. However, comparing line by line is not an option as the same email in list1 can exist in list2 but in a different row number.

I'm using this method:

F1 = open("c:\\FILEA.txt", "r").read().split('\n')
F2 = open("c:\\FILEB.txt", "r").read().split('\n')

lines1 = filter(None, (line.rstrip() for line in sorted([n.lower() for n in F1])))
lines2 = filter(None, (line.rstrip() for line in sorted([n.lower() for n in F2])))

for i in ( i for i in lines1 if lines2[:2] == lines1[:2]):
    print i

The above is just an example but only compares line by line. Does anyone have any idea how to compare each email from list1 and see if the email exists in list2.

Many thanks

share|improve this question
add comment

1 Answer

If you're just looking to see whether one is in the other (and don't care about frequency, etc.), you could try using set's to store unique occurrences from each file, and then find the intersection of the two sets, which would represent emails that were present in both files (note that the with statement with two files is a Python2.7+ feature):

>>> l1 = set()
>>> l2 = set()
>>> with open('FILEA.txt', 'rb') as f1, open('FILEB.txt', 'rb') as f2:
...     for line in f1.readlines():
...         l1.add(line.strip())
...     for line in f2.readlines():
...         l2.add(line.strip())
>>> l1
set(['another@gmail.com', 'andanother@hotmail.com', 'this@email.com'])
>>> l2
set(['unique@somehost.com', 'this@email.com', 'not@example.com'])
>>> l1 & l2

With sets, you can also perform other (potentially) helpful operations:

Identify items that are in both sets (union):

>>> l1 | l2
set(['another@gmail.com', 'unique@somehost.com', 'andanother@hotmail.com', 'this@email.com', 'not@example.com'])

Items that are in one set but not the other (difference):

>>> l1 - l2
set(['another@gmail.com', 'andanother@hotmail.com'])
>>> l2 - l1
set(['not@example.com', 'unique@somehost.com'])

Items that are unique to each set (think of it as the union less the intersection) (symmetric_difference):

>>> l1 ^ l2
set(['another@gmail.com', 'not@example.com', 'unique@somehost.com', 'andanother@hotmail.com'])

Lastly, you can also perform those operations using methods instead of the operators. To use the methods, take a set, append one of the names in parentheses above and make the other set the argument:

>>> l1.intersection(l2)

My files looked like this:




share|improve this answer
Thats great, many thanks for that! I never thought enough about sets. However, for some reason there is 2 identical emails in each list and when I do an intersection I only get one of them, not both. –  tony marziano Dec 14 '12 at 22:03
@tonymarziano No problem :) Are you saying that there are two total duplicates (let's say Email1 and Email2) that exist in both lists, but when you do intersection you are only getting Email1? If so, have you verified that there is no leading whitespace? rstrip will only strip characters from the end of a string - not the beginning. –  RocketDonkey Dec 14 '12 at 22:21
add comment

Your Answer


