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()