0

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.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
Dan
  • 359
  • 5
  • 17
  • Ideas of where to start: work out how to iterate over a dict (list2), work out how to find a dict in a list (list1) where the dict has a given key:value pair (VLAN:xyz), work out how to append an item to a list (the OUI list). – jarmod May 12 '20 at 21:50
  • I would start with [How to iterate over a list of dictionaries](https://stackoverflow.com/questions/9152431/iterating-over-list-of-dictionaries). – Lord Elrond May 12 '20 at 21:51
  • Checking both those out. Thank you! – Dan May 12 '20 at 21:52

3 Answers3

1

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']}
]
Saadi Toumi Fouad
  • 2,779
  • 2
  • 5
  • 18
0

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.

0

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']}]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75