Convert ordered dict list to nested lists

I have a list of ordered dict that includes some duplicate identifiers in the data .. something like this

[OrderedDict([('caseId', 20), ('userId', 1), ('emailStatus', 21)]), 
 OrderedDict([('caseId', 20), ('userId', 1), ('emailStatus', 20)]), 
 OrderedDict([('caseId', 18), ('userId', 4), ('emailStatus', 21)]), 
 OrderedDict([('caseId', 19), ('userId', 3), ('emailStatus', 21)]), 
 OrderedDict([('caseId', 18), ('userId', 1), ('emailStatus', 20)]),
 OrderedDict([('caseId', 20), ('userId', 3), ('emailStatus', 21)]),
 OrderedDict([('caseId', 18), ('userId', 4), ('emailStatus', 20)]), 
 OrderedDict([('caseId', 19), ('userId', 1), ('emailStatus', 20)])]

I want to get a list of nested lists, something like this:

[{
"caseId": "20",
"users": [
  {
    "userId": "1",
    "emailStatus": [
      {
      "emailStatus" : "20"
      },
      {
      "emailStatus" : "21"
      }

    ]
  },
    {
    "userId": "3",
    "emailStatus": [
      {
      "emailStatus" : "21"
      }

    ]

  }
]
},
{
"caseId": "19",
"users": [
  {
    "userId": "1",
    "emailStatus": [
      {
      "emailStatus" : "20"
      }
    ]

  },
    {
    "userId": "3",
    "emailStatus": [
      {
      "emailStatus" : "21"
      }

    ]

  }
]
},
{
"caseId": "18",
"users": [
  {
    "userId": "1",
    "emailStatus": [
      {
      "emailStatus" : "20"
      }
    ]

  },
    {
    "userId": "4",
    "emailStatus": [
      {
      "emailStatus" : "20"
      },
      {
      "emailStatus" : "21"
      }

    ]

  }
]
}
]

Represents a nested list like this;

enter image description here

I tried to achieve this, iterate over both lists, but could not figure out how to record previous and next records of the same data. This is so confusing. If anyone can give me a start that I can repeat my list, that would be very kind of you.

Yours faithfully..

Updated Question

More detailed question here

+3
source share
1 answer

dict.setdefault dict:

temp = {}
for d in lst:
    temp.setdefault(d["caseId"], {}).setdefault(d["userId"], []).append(d["emailStatus"])
print(temp)
# {18: {1: [20], 4: [21, 20]}, 19: {1: [20], 3: [21]}, 20: {1: [21, 20], 3: [21]}}

collections.defaultdict:

temp = defaultdict(lambda: defaultdict(list))
for d in lst:
    temp[d["caseId"]][d["userId"]].append(d["emailStatus"])

dict list :

res = [{"caseId": case, "users": [{"userId": user, "emailStatus": [{"emailStatus": s} for s in status]} 
                                  for user, status in users.items()]} 
       for case, users in temp.items()]
print(res)
# [{'caseId': 18, 'users': [{'userId': 1, 'emailStatus': [{'emailStatus': 20}]}, {'userId': 4, 'emailStatus': [{'emailStatus': 21}, {'emailStatus': 20}]}]},
#  {'caseId': 19, 'users': [{'userId': 1, 'emailStatus': [{'emailStatus': 20}]}, {'userId': 3, 'emailStatus': [{'emailStatus': 21}]}]},
#  {'caseId': 20, 'users': [{'userId': 1, 'emailStatus': [{'emailStatus': 21}, {'emailStatus': 20}]}, {'userId': 3, 'emailStatus': [{'emailStatus': 21}]}]}]
+9

All Articles