I'm trying to cross check two nested list values, and if I get a match, append the values from list1 into list2.
For example;
list1 = [[ip, hostname], [ip, hostname], [ip, hostname]]
list2 = [[ip, ip_upper, type, hostname, location], [ip, ip_upper, type, hostname, location], [ip, ip_upper, type, hostname, location]]
I want to check if the value in list1[x][0]
is in list2
, if so, replace the value of List2[x][3]
with List1[x][1]
.
What I'm attempting is;
count = 0;
for row in list2:
if row[0] in hosts[count][0]:
new_hostname = hosts[count][1]
row[4].append(new_hostname)
count += 1
else:
continue
count += 1
I know the above is incorrect, i'm struggling to figure out how to access the values list1 whilst traversing list2. The reason is because I need to check each row, and then the value with the row and then amend specific values within the row.
Thanks for the RAPID response!
I've tried to implement the code given but am running into trouble when trying to create a diction from my list:
wow, you guys!
def doStuff(list1, list2):
mydic = dict(list2)
for l in list1:
l[3] = mydic.get(l[0], l[3])
return mydic
new_dic = doStuff(hostname, old_rows)
I'm receiving the error;
mydic = dict(list2)
ValueError: dictionary update sequence element #0 has length 6; 2 is required
Any ideas?
Assuming the
ip
field value supports equality comparison (==), this solution may help you. It's quite plain and lame and probably not very optimised, but it works.Note: it works for every occurrence of each element of list1 in list2, so if you had multiple matches in list2, they would all get updated.
outputs: