-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', 'john@domain.com'],
         ['hostname-b', 'joe', 'joe@domain.com'],
         ['hostname-c', 'jane', 'jane@domain.com'],
         ['hostname-d', 'bob', 'bob@domain.com']]

list2 = [['hostname-a', 'sally', 'sally@domain.com'],
         ['hostname-b', 'harry', 'harry@domain.com']]

Targeted Output:

missing_devices = [['hostname-c', 'jane', 'jane@domain.com'],
                  ['hostname-d', 'bob', 'bob@domain.com']]
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()
Kevlar_Axis
  • 65
  • 1
  • 9
  • [How to compare a list of lists/sets in python?](https://stackoverflow.com/a/6105826) – 001 Jul 26 '22 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. – Kevlar_Axis Jul 26 '22 at 22:15

1 Answers1

0

list1 = [['hostname-a', 'john', 'john@domain.com'],
         ['hostname-b', 'joe', 'joe@domain.com'],
         ['hostname-c', 'jane', 'jane@domain.com'],
         ['hostname-d', 'bob', 'bob@domain.com']]

list2 = [['hostname-a', 'sally', 'sally@domain.com'],
         ['hostname-b', 'harry', 'harry@domain.com']]

missing_devices = [['hostname-c', 'jane', 'jane@domain.com'],
                   ['hostname-d', 'bob', 'bob@domain.com']]

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', 'jane@domain.com'], ['hostname-d', 'bob', 'bob@domain.com']]
"""

Xin
  • 22
  • 4