sonali
sonali

Reputation: 229

Nested Dictionary using python

I am trying to write nested type of dictionary in python. I am providing my input and expected output and my tried code.

This is my input:

input = [['10', 'PS_S1U_X2_LP', 'permit', 'origin', 'igp', 'RM_S1U_X2_LP'],
['20', '', 'permit', '', '', 'RM_S1U_X2_LP'], 
['10', 'MPLS-LOOPBACK', 'permit', '', '', 'MPLS-LOOPBACK-RLFA'], 
]

And my desired output is:

output = 
"route_policy_list": [
{
          "policy_terms": [],
          "route_policy_statement": [
            {
              "entry": "10",
              "prefix_list": "PS_S1U_X2_LP",
              "action_statements": [
                {
                  "action_value": "igp",
                  "action": "permit",
                  "action_statement": "origin"
                }
              ]
            },
            {
                "entry": "20",
                "prefix_list": "",
                "action_statements": [
                  {
                    "action_value": "",
                    "action": "permit",
                    "action_statement": ""
                  }
                ]  
            }
          ],
          "name": "RM_S1U_X2_LP"
},


        {
          "policy_terms": [],
          "route_policy_statement": [
            {
              "entry": "10",
              "prefix_list": "MPLS-LOOPBACK",
              "action_statements": [
                {
                  "action_value": "",
                  "action": "permit",
                  "action_statement": ""
                }
              ]
            }
          ],
          "name": "MPLS-LOOPBACK-RLFA"
        } 
]

And I have tried this code:

from collections import defaultdict

res1 = defaultdict(list)
for fsm1 in input:
    name1 = fsm1.pop()
    action = fsm1[2]
    action_statement = fsm1[3]
    action_value = fsm1[4]
    item1 = dict(zip(['entry','prefix_list'],fsm1))
    res1['action'] = action
    res1['action_statement'] = action_statement
    res1['action_value'] = action_value
    res1[name].append(item1)

print(res1)

Please help me to get desired output as mentioned above as i am new to coding and struggling to write.

Upvotes: 1

Views: 64

Answers (2)

Akash Swain
Akash Swain

Reputation: 520

Here is the final code. I used setdefault method to group the data first then used simple for loop to represent the data in requested way.

# Input
input = [['10', 'PS_S1U_X2_LP', 'permit', 'origin', 'igp', 'RM_S1U_X2_LP'],
['20', '', 'permit', '', '', 'RM_S1U_X2_LP'], 
['10', 'MPLS-LOOPBACK', 'permit', '', '', 'MPLS-LOOPBACK-RLFA'], 
]

# Main code
d = {}
final = []
for i in input:
    d.setdefault(i[-1], []).append(i[:-1])

for i, v in d.items():
    a = {}
    a["policy_terms"] = []
    a["route_policy_statement"] = [{"entry": j[0], "prefix_list":j[1], "action_statements":[{"action_value":j[-2], "action": j[-4], "action_statement": j[-3]}]} for j in v]
    a["name"] = i
    final.append(a)

final_dict = {"route_policy_list": final} 
print (final_dict)

# Output
# {'route_policy_list': [{'policy_terms': [], 'route_policy_statement': [{'entry': '10', 'prefix_list': 'PS_S1U_X2_LP', 'action_statements': [{'action_value': 'origin', 'action': 'PS_S1U_X2_LP', 'action_statement': 'permit'}]}, {'entry': '20', 'prefix_list': '', 'action_statements': [{'action_value': '', 'action': '', 'action_statement': 'permit'}]}], 'name': 'RM_S1U_X2_LP'}, {'policy_terms': [], 'route_policy_statement': [{'entry': '10', 'prefix_list': 'MPLS-LOOPBACK', 'action_statements': [{'action_value': '', 'action': 'MPLS-LOOPBACK', 'action_statement': 'permit'}]}], 'name': 'MPLS-LOOPBACK-RLFA'}]}

I hope this helps and count!

Upvotes: 1

Ronny Efronny
Ronny Efronny

Reputation: 1498

It seems like every sublist in input consists of the same order of data, so I would create another list of indices such as

indices = ['entry', 'prefix_list', 'action', 'action_statement', 'action_value', 'name']

and then just hard code the values, because it seems you want specific values in specific places.

dic_list = []
for lst in input:
    dic = {'policy terms' : [],
           'route_policy_statements' : {
               indices[0] : lst[0],
               indices[1] : lst[1],
               'action_statements' : {
                   indices[2] : lst[2],
                   indices[3] : lst[3],
                   indices[4] : lst[4]
               },
               indices[5] : lst[5]
           }
           }

    dic_list.append(dic)

Upvotes: 0

Related Questions