1

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.

1
  • 1
    what is your result of that function you write? Commented Nov 5, 2019 at 8:59

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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)

3 Comments

I guess there is a small gap here. You missed grouping the data by name(last field of each inner list)
There was nothing really grouped in the original, you just had 3 lists with the final item in each list being a designated "name" so I just put it as such.
I guess I was not able to ask my question correctly. If you see the expected output, the 3 lists are grouped into 2 by --> 1. "name": "RM_S1U_X2_LP" and 2. "name": "MPLS-LOOPBACK-RLFA". Your result has a list of 3 dictionaries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.