Dan
Dan

Reputation: 359

Python3 - Compare two lists of dictonaries and update value

I have two lists (these will be hundreds of records long) but here are samples:

list1 = [
{'VENDOR': 'VENDOR1', 'VLAN': '500', 'OUI': []},
{'VENDOR': 'VENDOR2', 'VLAN': '600', 'OUI': []},
{'VENDOR': 'VENDOR3', 'VLAN': '700', 'OUI': []},
]

list2 = [
{'VLAN': '500', 'OUI': '0001FC'},
{'VLAN': '600', 'OUI': '00D024'},
{'VLAN': '500', 'OUI': '00D024'},
{'VLAN': '700', 'OUI': '00D024'},
{'VLAN': '700', 'OUI': '023456'},
]

I want to take out every OUI from list2 where the VLAN matches between the two lists, then add it to the value for the OUI key in list1. (I imagine this may require a 3rd dict to hold everything).

The end result would be something like:

list3 = [
{'VENDOR': 'VENDOR1', 'VLAN': '500', 'OUI': ["0001FC", "00D024"]},
{'VENDOR': 'VENDOR2', 'VLAN': '600', 'OUI': ["00D024"]},
{'VENDOR': 'VENDOR3', 'VLAN': '700', 'OUI': ["00D024", "023456"]}
]

I'm not even sure where to start with this one, so any help is appreciated.

Upvotes: 0

Views: 65

Answers (3)

RoadRunner
RoadRunner

Reputation: 26315

I would restructure list1 to be a nested dictionary, where VLAN is the key, then iterate list2 and add the VLAN items to the OUI lists in the nested dict. Then we can just print the values() of the nested dict.

list1 = [
    {"VENDOR": "VENDOR1", "VLAN": "500", "OUI": []},
    {"VENDOR": "VENDOR2", "VLAN": "600", "OUI": []},
    {"VENDOR": "VENDOR3", "VLAN": "700", "OUI": []},
]

list2 = [
    {"VLAN": "500", "OUI": "0001FC"},
    {"VLAN": "600", "OUI": "00D024"},
    {"VLAN": "500", "OUI": "00D024"},
    {"VLAN": "700", "OUI": "00D024"},
    {"VLAN": "700", "OUI": "023456"},
]

vendors = {v["VLAN"]: v for v in list1}
# {'500': {'VENDOR': 'VENDOR1', 'VLAN': '500', 'OUI': []}, '600': {'VENDOR': 'VENDOR2', 'VLAN': '600', 'OUI': []}, '700': {'VENDOR': 'VENDOR3', 'VLAN': '700', 'OUI': []}}

for vlan in list2:
    vendors[vlan["VLAN"]]["OUI"].append(vlan["OUI"])

print(list(vendors.values()))

Output:

[{'VENDOR': 'VENDOR1', 'VLAN': '500', 'OUI': ['0001FC', '00D024']}, {'VENDOR': 'VENDOR2', 'VLAN': '600', 'OUI': ['00D024']}, {'VENDOR': 'VENDOR3', 'VLAN': '700', 'OUI': ['00D024', '023456']}]

Upvotes: 0

Saadi Toumi Fouad
Saadi Toumi Fouad

Reputation: 2829

Here is a way to do that, and about

(I imagine this may require a 3rd dict to hold everything)

that's not necessary

list1 = [
  {'VENDOR': 'VENDOR1', 'VLAN': '500', 'OUI': []},
  {'VENDOR': 'VENDOR2', 'VLAN': '600', 'OUI': []},
  {'VENDOR': 'VENDOR3', 'VLAN': '700', 'OUI': []},
]

list2 = [
  {'VLAN': '500', 'OUI': '0001FC'},
  {'VLAN': '600', 'OUI': '00D024'},
  {'VLAN': '500', 'OUI': '00D024'},
  {'VLAN': '700', 'OUI': '00D024'},
  {'VLAN': '700', 'OUI': '023456'},
]

# to hold indexes of VLAN in list1 so it will be easy to 
# append to the right "OUI" list inside list1
indexes = {}

# get the index of each VLAN
for i in range(len(list1)):
  indexes[list1[i]["VLAN"]] = i
# `indexes` now => {"500": 0, "600": 1, "700": 2}
for a in list2:
  # now it's easy to target the right `"OUI"` list
  # for example the first iteration
  # list1[0]["OUI"].append("0001FC")
  list1[indexes[a["VLAN"]]]["OUI"].append(a["OUI"])

print(list1)

Output:

[
  {'VENDOR': 'VENDOR1', 'VLAN': '500', 'OUI': ['0001FC', '00D024']},
  {'VENDOR': 'VENDOR2', 'VLAN': '600', 'OUI': ['00D024']},
  {'VENDOR': 'VENDOR3', 'VLAN': '700', 'OUI': ['00D024', '023456']}
]

Upvotes: 1

ammar albakri
ammar albakri

Reputation: 78

the code you will be needing to do that operation is here:

 for i in range(0,len(list1)):
      for j in range(0,len(list2)):
          if(list1[i]["VLAN"] == list2[j]["VLAN"]):
              list1[i]["OUI"].append(list2[j]["OUI"])

and to test if you got correct outputs you can use this code:

 for i in range(0,len(list1)):
        print(list1[i]["OUI"])

I believe this code is self-explanatory but still if you want an explanation as to why it is like this please feel free to ask.

Upvotes: 0

Related Questions