-1

I have 2 csv files. I am reading each of them into 2 separate list of lists.

Example: Each csv has 3 columns ['Device', 'User', 'Email']

Edit: I need to keep each list of 3 elements ONLY if element[0] does not match in both lists

list1 = [['hostname-a', 'john', '[email protected]'],
         ['hostname-b', 'joe', '[email protected]'],
         ['hostname-c', 'jane', '[email protected]'],
         ['hostname-d', 'bob', '[email protected]']]

list2 = [['hostname-a', 'sally', '[email protected]'],
         ['hostname-b', 'harry', '[email protected]']]

Targeted Output:

missing_devices = [['hostname-c', 'jane', '[email protected]'],
                  ['hostname-d', 'bob', '[email protected]']]
import csv, re

def start():
    file1 = "C:/path/to/file/file1.csv"
    file2 = "C:/path/to/file/file2.csv"
    
    list1 = list_1(file1)
    list2 = list_2(file2)
        
    ## This is where I seem to be hung up. This list comprehension isn't working, but hope it shows what I am trying to accomplish
    diff_list = [x for x[0] in list2 if x[0] not in list1]

    filename = "devices_missing_from_list_2.csv"
    output_csv(diff_list, filename)

def list_1(file1):
    with open(file1, "r") as file1:
        devices = csv.DictReader(file1, delimiter=',')
        list1 = []
        for x in devices:
            host = x["Device"]
            user = x["User"]
            email = x["Email"]
            host = host.lower()
            list1.append([host, user, email])
    return list1
def list_2(file2):
    with open(file2, "r") as file2:
        devices = csv.DictReader(file2, delimiter=',')
        list2 = []
        for x in devices:
            host = x["Device"]
            user = x["User"]
            email = x["Email"]
            host = host.lower()
            list2.append([host, user, email])
    return list2
           
def output_csv(diff_list, filename):
    with open(filename, 'w', encoding='utf-8', newline='') as outfile:
        header = ['Device', 'User', 'Email']
        writer = csv.writer(outfile)
        writer.writerow(header)
        for row in server_list:
            writer.writerow([row[0], row[1], row[2]])

if __name__ == "__main__":
    start()
2
  • How to compare a list of lists/sets in python? Commented Jul 26, 2022 at 20:13
  • Thank you for editing to clean up my post. I've reviewed the link and it is answering based on the full lists of 3 elements being identical to "match" on the differences. My issue is I need to match only on the first element while keeping the full list of 3 elements when iterating through the list of lists. Now that I'm re-reading my post, I realize I did not do a good job of explaining that properly. I will edit to clarify. Commented Jul 26, 2022 at 22:15

1 Answer 1

0

list1 = [['hostname-a', 'john', '[email protected]'],
         ['hostname-b', 'joe', '[email protected]'],
         ['hostname-c', 'jane', '[email protected]'],
         ['hostname-d', 'bob', '[email protected]']]

list2 = [['hostname-a', 'sally', '[email protected]'],
         ['hostname-b', 'harry', '[email protected]']]

missing_devices = [['hostname-c', 'jane', '[email protected]'],
                   ['hostname-d', 'bob', '[email protected]']]

first_set_1 = {i[0] for i in list1}
first_set_2 = {i[0] for i in list2}

diff_set = first_set_1.difference(first_set_2)
print(diff_set)
final_list = [i for i in list1 if i[0] in diff_set]
print(final_list)

"""
{'hostname-d', 'hostname-c'}
[['hostname-c', 'jane', '[email protected]'], ['hostname-d', 'bob', '[email protected]']]
"""

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.