iterate and compare two lists in python

Viewed 181

I am still pretty new to coding and python so I am not sure what is the best way to iterate and compare two lists.

  • If list2 first name, last name and id are different than list1, then append to line from list2 to list1.
  • If list2 first name, last name and id are the same as list1, then append to the existing line.

Below is what I have so far... I can't figure the part to append new rows to list1.

list1 = [["John","Smith","ID100",1000.00,50000.00],
     ["Jane","Doe","ID200",2000.00,30000.00],
     ["Joe","Dirt","ID300",20000.00,300000.00]]

list2 = [["John","Smith","ID100",23222.00,123444.00],
     ["Jane","Doe","ID200",65000,70098.00],
     ["Dale","Wright","ID400",25000.00,126000.00],
     ["John","Clark","ID500",23002.00,12111.00]]

for i in range(len(list1)):
    for j in range(len(list2)):
        if list1[i][0] == list2[j][0] and list1[i][1] == list2[j][1] and list1[i][2] == list2[j][2]:       
            count4=3
            for k in range(len(list2[0][3:])):
                list1[i].append(list2[j][count4])
                count4+=1
        else:
            list1.append(list2[i])

print(list1)

Result:

[['John', 'Smith', 'ID100', 1000.0, 50000.0, 23222.0, 123444.0], 
 ['Jane', 'Doe', 'ID200', 2000.0, 30000.0, 65000, 70098.0], 
 ['Joe', 'Dirt', 'ID300', 20000.0, 300000.0], 
 ['John', 'Smith', 'ID100', 23222.0, 123444.0], 
 ['John', 'Smith', 'ID100', 23222.0, 123444.0], 
 ['John', 'Smith', 'ID100', 23222.0, 123444.0], 
 ['Jane', 'Doe', 'ID200', 65000, 70098.0], 
 ['Jane', 'Doe', 'ID200', 65000, 70098.0], 
['Jane', 'Doe', 'ID200', 65000, 70098.0], 
['Dale', 'Wright', 'ID400', 25000.0, 126000.0], 
['Dale', 'Wright', 'ID400', 25000.0, 126000.0], 
['Dale', 'Wright', 'ID400', 25000.0, 126000.0], 
['Dale', 'Wright', 'ID400', 25000.0, 126000.0]]

Result that I want...

[['John','Smith','ID100',1000.00,50000.00,23222.00,123444.00],
['Jane','Doe','ID200',2000.00,30000.00,65000,70098.00],
['Joe','Dirt','ID300',20000.00,300000.00],
['Dale','Wright','ID400',25000.00,126000.00],
['John','Clark','ID500',23002.00,12111.00]]
2 Answers

You can do the following to get the desired result.
I'm using a dict to track unique tuples of (Fname, Lname, ID) as keys and the value would be default an empty list and I would add the required data as I encounter lines with the same key. This can be efficiently done using setdefault and extend functions. Check the code below:

list1 = [["John","Smith","ID100",1000.00,50000.00],
        ["Jane","Doe","ID200",2000.00,30000.00],
        ["Joe","Dirt","ID300",20000.00,300000.00]]

list2 = [["John","Smith","ID100",23222.00,123444.00],
        ["Jane","Doe","ID200",65000,70098.00],
        ["Dale","Wright","ID400",25000.00,126000.00],
        ["John","Clark","ID500",23002.00,12111.00]]

temp = dict()

for entry in (list1+list2):
    temp.setdefault(tuple(entry[:3]), []).extend(entry[3:])

res = []
for i, j in temp.items():
    res.append(list(i)+j)
print(res)

Output:

[['John', 'Smith', 'ID100', 1000.0, 50000.0, 23222.0, 123444.0],
 ['Jane', 'Doe', 'ID200', 2000.0, 30000.0, 65000, 70098.0], 
 ['Joe', 'Dirt', 'ID300', 20000.0, 300000.0], 
 ['Dale', 'Wright', 'ID400', 25000.0, 126000.0], 
 ['John', 'Clark', 'ID500', 23002.0, 12111.0]]

In if statement you added list2[i] to list1 but you must add list2[j] to list1:

...
else
    list1.append(list2[i])
...
Related