Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare values from multiple dictionaries

I have two lists of dictionaries and a tuple:

vrfs = [
 {'default_rd': '<not set>',
  'interfaces': ['Gi0/0'],
  'name': 'Mgmt-vrf',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:510',
  'interfaces': ['503', '510', '515'],
  'name': 'VLAN1',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:993',
  'interfaces': ['993'],
  'name': 'VLAN2',
  'protocols': 'ipv4,ipv6'}
]

my_dict = [
 {'Device Name': '',
  'Hostname': 'switch1',
  'IP Address': '',
  'Interface Number': 'Gi1/0/2',
  'MAC': 'A0:B1:C2:D3:E4:F5',
  'VLAN': '503'},
 {'Device Name': '',
  'Hostname': 'switch1',
  'IP Address': '',
  'Interface Number': 'Gi1/0/3',
  'MAC': 'A1:B2:C3:D4:E5:F6',
  'VLAN': '510'},
 {'Device Name': '',
  'Hostname': 'switch1',
  'IP Address': '',
  'Interface Number': 'Gi1/0/4',
  'MAC': 'A2:B3:C4:D5:E6:F7',
  'VLAN': '515'},
 {'Device Name': '',
  'Hostname': 'switch1',
  'IP Address': '',
  'Interface Number': 'Gi1/0/5',
  'MAC': 'A3:B4:C5:D6:E7:F8',
  'VLAN': '993'},
 {'Device Name': '',
  'Hostname': 'switch1',
  'IP Address': '',
  'Interface Number': 'Gi1/0/6',
  'MAC': 'A4:B5:C6:D7:E8:F9',
  'VLAN': '750'}
]

vlans = ('Gi0/0', '503', '510', '515', '993')

And I need to iterate through my_dict but if my_dict["VLAN"] is in vlans tuple then I need search in vrfs and return the vrfs["name"].

So something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if vlan in vlans:
    print(f"show ip arp vrf {vrfs['name']} {my_dict}['MAC']")
else:
    print(f"show ip arp {my_dict}['MAC']")

How would I accomplish this?

>Solution :

Your question is not clear but I do what I understand.

for a in my_dict:
    if a['VLAN'] in vlans:
        for i in vrfs:
            if a['VLAN'] in i['interfaces']:
                print(i['name'])

OUTPUT:

VLAN1
VLAN1
VLAN1
VLAN2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading