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]]