How to split the dictionary with in list [closed]

Dictionary is below

{ "id": "234", "name": "Manger", "policies": [ { "id": "13", "name": "P"}, { "id": "14", "name": "PMO" } ]}

I need to split into two whilie printing

{ "id": "234", "name": "Manger", "policies":  { "id": "13", "name": "P"}
{ "id": "234", "name": "Manger", "policies":  { "id": "14", "name": "PMO" } 

Answer

You can iterate through your policies prepreti in your initial dictionary.

old_dict={ "id": "234", "name": "Manger", "policies": [ { "id": "13", "name": "P"}, { "id": "14", "name": "PMO" } ]}
new_dict = []
for policy in old_dict["policies"]:
  tobe_add = old_dict.copy()
  tobe_add["policies"] = policy
  new_dict.append(tobe_add)

print(new_dict)

Leave a Reply

Your email address will not be published. Required fields are marked *