'Parsing OrderedDict to Python list

I am using a module called Simple Salesforce to query data from my Salesforce database. The data comes back as an OrderedDict. How can I parse this into simple python lists.

Sample of the first two entries as it is returned when queried:

OrderedDict([
    (u'totalSize', 418), 
    (u'done', True), 
    (u'records', [
        OrderedDict([
            (u'attributes', OrderedDict([
                (u'type', u'Case'), 
                (u'url', u'/services/Case/11111')
                ])), 
            (u'Id', u'11111'), 
            (u'Subject', u'Case 1')
            ]), 
        OrderedDict([
            (u'attributes', OrderedDict([
                (u'type', u'Case'), 
                (u'url', u'/services/Case/2222222')
                ])), 
            (u'Id', u'2222222'), 
            (u'Subject', u'Case 2')
            ]),
        #...
        ])
    ])

I not sure I have the term list correct, but I want it as a multi-dimensional table in the form of:

[('11111', 'Case 1'),('2222222', 'Case 2')]

Ultimately, I'd like to inner join this list with another list. Is this the best way to set up the data?

So the two lists that I would like to inner join would be :

List 1:

List1 = [('11111', 'Case 1'),('2222222', 'Case 2')] # [ID, Subject]

List 2:

List2 = [('11111', 'April'),('2222222', 'March'),('333333', 'January')]  # [ID, Date]

Desired Output:

[('11111', 'Case 1','April'),('2222222', 'Case 2','March')]


Solution 1:[1]

What you have is a dict that contains, among other things, other dicts. Extrapolating from your desired output, what I believe you want is to convert this data structure into a list containing the Id and Subject of each element under the top-level records key.

Now that we've defined the requirement, the solution presents itself easily: loop over the records list and make tuples containing the desired properties. Let's say that top-level object is called data. Then:

output = []
for record in data['records']:  # Loop over all the records (each one an OrderedDict) in the list
    subject = record['Subject']  # Extract the required information
    identifier = record['Id']
    output.append((identifier, subject))  # Add the extracted info to the output list

print(output)  # Prints: [('11111', 'Case 1'), ('2222222', 'Case 2')]

Once you're comfortable with the basic idea, you can actually condense this loop into a list comprehension, which may be much faster to create if your input is large. The following line is equivalent to the loop above, but cleaner to read (for someone familiar with Python structures, anyway).

output = [(record['Id'], record['Subject']) for record in data['records']]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1