Home > Mobile >  Python - Edit and remove values in a list of dictionaries
Python - Edit and remove values in a list of dictionaries

Time:05-10

I have a list of dictionaries where I need to remove anything from the interfaces keys that is Po...... and I need to remove the Vl from the Vlan interfaces, just keeping the Vlan number. I need to change the following list of dictionaries:

vrfs = [
 {'default_rd': '<not set>',
  'interfaces': ['Gi0/0'],
  'name': 'Mgmt-vrf',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:510',
  'interfaces': ['Po31.510', 'Po32.510', 'Vl503', 'Vl510', 'Vl515'],
  'name': 'VLAN1',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:993',
  'interfaces': ['Po31.993', 'Po32.993', 'Vl993'],
  'name': 'VLAN2',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:855',
  'interfaces': ['Po31.855', 'Po32.855', 'Vl855'],
  'name': 'VLAN3',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:266',
  'interfaces': ['Po31.266', 'Po32.266', 'Vl266'],
  'name': 'VLAN4',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:248',
  'interfaces': ['Po31.248', 'Po32.248', 'Vl248'],
  'name': 'VLAN5',
  'protocols': 'ipv4,ipv6'}
]

To look like this:

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'},
 {'default_rd': '12345:855',
  'interfaces': ['855'],
  'name': 'VLAN3',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:266',
  'interfaces': ['266'],
  'name': 'VLAN4',
  'protocols': 'ipv4,ipv6'},
 {'default_rd': '12345:248',
  'interfaces': ['248'],
  'name': 'VLAN5',
  'protocols': 'ipv4,ipv6'}
]

What's the best way to accomplish this?

CodePudding user response:

Try:

for d in vrfs:
    d["interfaces"] = [v.replace("Vl", "") for v in d["interfaces"] if not v.startswith("Po")]

print(vrfs)

Prints:

[
    {
        "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",
    },
    {
        "default_rd": "12345:855",
        "interfaces": ["855"],
        "name": "VLAN3",
        "protocols": "ipv4,ipv6",
    },
    {
        "default_rd": "12345:266",
        "interfaces": ["266"],
        "name": "VLAN4",
        "protocols": "ipv4,ipv6",
    },
    {
        "default_rd": "12345:248",
        "interfaces": ["248"],
        "name": "VLAN5",
        "protocols": "ipv4,ipv6",
    },
]

CodePudding user response:

It seems that you are just trying to change the 'interfaces' key in each dictionary item in the list. Below is a code that iterates through every dictionary item in the list and modifies the interfaces key.

for dic in vrfs:
    interfaces = dic.get('interfaces', None)
    if interfaces:
        # iterate through items in interfaces
        res = []
        for item in interfaces:
            if item[:2] == "Po":
                # ignore items that start with Po
                continue
            elif item[:2] == "Vl":
                # ignore the 'Vl' part
                res.append(item[2:])
            else:
                res.append(item)
        dic['interfaces'] = res

CodePudding user response:

I personally would recommend using a set cause it prevents repeats.

for d in vrfs:
  temp_set=set()
  for i in d["interfaces"]:
    if(i[0:2]=="Po"):
      temp_set.add(i[5:])
    elif(i[0:2]=="Vl"):
      temp_set.add(i[2:])
    else:
      temp_set.add(i)
  d["interfaces"]=sorted(temp_set)

print(vrfs)

CodePudding user response:

This technically work as you want but it needs (as any code) adaptations to new interfaces syntax.

for vrf in vrfs:
    interfaces = vrf['interfaces']
    vrf['interfaces'] = []
    for interface in interfaces:
        if ('.' and 'Po') in interface: new_interface = interface.split('.')[1]
        elif 'Vl' in interface: new_interface = interface[2:]
        else: new_interface = interface
        if new_interface not in vrf['interfaces']: vrf['interfaces'].append(new_interface)
  • Related