Home > Back-end >  python how to put difference of 2 files in a list?
python how to put difference of 2 files in a list?

Time:10-07

I have 2 files with emailadresses in them and some of these emailadresses are the same and some aren't. I need to see which of the emailadresses in file1 aren't in file2. How can I do that? Also it would be great if I can put them in a list too.

here's what I got:

    'file1 = open("competitor_accounts.txt")
     file2 = open("accounts.txt")'

I know it ain't much, but I need help getting started

I thought maybe using a for loop with if statements? but I just don't know how.

CodePudding user response:

You can read each file's contents to a separate list and then compare the lists to each other like so

with open('accounts.txt') as f:
    accounts = [line for line in f]

with open('competitor_accounts.txt') as f:
    competitors = [line for line in f]

accounts_not_competitors = [line for line in accounts if line not in competitors]
competitors_not_accounts = [line for line in competitors if line not in accounts]

You can use open as well with readlines() but using with is commonly more acceptable since you don't need to explicitly close() the file after you're done reading it.

file_a = open('accounts.txt')
accounts = file_a.readlines()
file_a.close()

The two rows in the end form an expression to generate a new list based on matches in the existing lists. These can be written out to an easier form:

accounts_not_competitors = []
for line in accounts:
    if line not in competitors:
        accounts_not_competitors.append(line)

I believe this should be enough to get you started with the syntax and functionality in case you wanted to do some other comparisons between the two.

CodePudding user response:

Assuming that only one email is a line in each file

First save each file in a list and create another list that you will save the difference in.

Loop through file1 list and check if each item in file1 list is present in file2 list if not add that item to the diff list

f1_list = []
f2_list = []
diff = []

with open(file1name, 'r', encoding='utf-8') as f1:
    for line in f1:
        f1_list.append(line)

with open(file2name, 'r', encoding='utf-8') as f2:
    for line in f2:
        f2_list.append(line)

for email in f1_list:
    if not email in f2_list:
         diff.append(email)

print(diff)
  • Related