-1

I have a list of dictionaries that need to be filled. It has a top level identifier for the Customer Service Case Number. I have another list of dictionaries that has all the email information related to the case - these dictionaries also have the case number that the emails are related to.

I am trying to break up the email list and associate them in the first list of cases.

I am pulling the information using the Simple Salesforce Package.

#First dictionary:

Cases =
    [{CaseNumber : "1",
     Date: "value",
     Reason: "value",
     Emails: NONE (need to add here)},
     {CaseNumber : "2",
     Date: "value",
     Reason: "value",
     Emails: NONE (need to add here)}]

#etc.  this list is a set of dictionaries of all the cases related to an order #number that is passed in previously.

#The second list of is a list of all of the emails related to the above cases -

Emails =
    [{CaseNumber : "1",
     Date: "value",
     EmailBody: "value",},
     {CaseNumber : "1",
     Date: "value",
     EmailBody: "value",},
     {CaseNumber : "2",
     Date: "value",
     EmailBody: "value",}]

#So this list needs to get nested accordingly to the Emails key by matching on case number

Cases =
    [{CaseNumber : "1",
     Date: "value",
     Reason: "value",
     Emails: emails[1,2,etc]},
     {CaseNumber : "2",
     Date: "value",
     Reason: "value",
     Emails: emails[3,4,etc]}]

Should create a list of nested dictionaries that are related to the top level case Id.

  • Possible duplicate https://stackoverflow.com/questions/48700710/python-flatten-the-list-of-dictionaries – KuboMD Jan 10 '19 at 18:04

1 Answers1

0

Might not be the fastest solution, but I'd go through the emails and group them based on CaseNumber. This would be a list of the emails based on that value. From this, you can go through each case, grab the CaseNumber, and insert the list into the Emails part of the dictionary

from collections import defaultdict

e = defaultdict(list)

for email in Emails:
    case = email.get('CaseNumber')
    e[case].append(email)

for case in Cases:
     casenumber = case.get('CaseNumber')
     case['Emails'] = e[casenumber]

# Cases now looks like [{'CaseNumber': '0', 'Emails': [{'EmailBody': 'value'},...], {'CaseNumber': '1', 'Emails': [{'EmailBody': 'value',...}]}]

defaultdict is a dictionary that will insert a list if a key is not present, allowing for the e[key].append() call without checking if the key is in the dictionary. The docs are here

EDIT:

In the interest of completeness, the way to do this without defaultdict would be as follows:

e = {}
for email in Emails:
    case = email.get('CaseNumber')
    if not case in e:
        # create single element list if key not in dictionary
        e[case] = [email]
    else:
        e[case].append(email)

In more shorthand, you can use get to avoid KeyErrors entirely:

for email in Emails:
    case = email.get('CaseNumber')
    e[case] = e.get(case, []).append(email)

Where get will return either the value associated with case or a default value specified as the second argument (which is a list here)

C.Nivs
  • 12,353
  • 2
  • 19
  • 44